I have to models with M2M relation which are clinic and Doctor but the third custom intermediate table with additional field shift.
class ClinicHospital(models.Model):
name = models.CharField(max_length = 256)
address = models.TextField()
contact = models.CharField(max_length = 15)
lat = models.FloatField()
lon = models.FloatField()
class Doctor(models.Model):
name = models.CharField(max_length = 256)
speciality = models.CharField(max_length = 256)
contact = models.CharField(max_length = 12)
speciality = models.ForeignKey(Speciality, on_delete=models.CASCADE)
clinic_hospital = models.ManyToManyField(ClinicHospital, through='DoctorHospital')
intermediate table is
class DoctorHospital(models.Model):
clinic = models.ForeignKey(ClinicHospital, on_delete=models.CASCADE)
doctor = models.ForeignKey(Doctor, on_delete=models.CASCADE)
shift = models.CharField(max_length = 10)
Problem
First, DoctorHospital table is not shown/created in DB.
Second, when I save Clinic it does but Doctor does not save, it returns error doctorhospital is not exist.
this error view show when save Doctor
Related
class Mio_terminal(models.Model):
terminal = models.CharField(max_length = 50)
gate = models.CharField(max_length = 50)
gate_status = models.CharField(max_length = 50, default = 'open') #open, occupied, under_maintenance
class Meta:
unique_together = [['terminal', 'gate']]
class Mio_flight_schedule(models.Model):
fact_guid = models.CharField(max_length=64, primary_key=True)
airline_flight_key = models.ForeignKey(Mio_airline, related_name = 'flight_key', on_delete = models.CASCADE)
source = models.CharField(max_length = 100)
destination = models.CharField(max_length = 100)
arrival_departure = models.CharField(max_length=12)
time = models.DateTimeField()
gate_code = models.ForeignKey(Mio_terminal, related_name = 'terminal_gate', null = True, on_delete = models.SET_NULL)
baggage_carousel = models.CharField(max_length = 100)
remarks = models.CharField(max_length = 100)
terminal_code = models.ForeignKey(Mio_terminal, related_name = 'airport_terminal', null = True, on_delete = models.SET_NULL)
These are models for terminal and flight schedules.
enter image description here
enter image description here
I want to have a terminal name and gate code instead of the object ...
I know we can get this by using the str method in models....but we get only a single value for this...not more than one
I want to use the terminal as a foreign_key for terminal_code in the flight_schedule model and the gate as a gate_code.I am getting terminal_code _gate code as a string ...but it's not reflecting as separate entities...I don't want the combined string.....I want when I click on the terminal ...only the terminal dropdown should display..and the same for the gate code
please let me know how should I deal with this.
You can change the display name of models in django admin interface by using def __str__(self).
So for in your code add this function after the Mio_terminal class :
class Mio_terminal(models.Model):
terminal = models.CharField(max_length = 50)
gate = models.CharField(max_length = 50)
gate_status = models.CharField(max_length = 50, default = 'open') #open, occupied, under_maintenance
class Meta:
unique_together = [['terminal', 'gate']]
# add this
def __str__(self):
return str(self.terminal+' '+self.gate)
I have 2 databases that are supposed to be linked by a foreign key: DealershipListing and Dealers.
Here are their models:
class Dealer(models.Model):
dealersName = models.TextField(('DealersName'))
zipcode = models.CharField(("zipcodex"), max_length = 15)
zipcode_2 = models.CharField(("zipCode"), max_length = 15)
state = models.CharField(("state"), max_length=5)
address = models.TextField(("Address"))
ids = models.BigIntegerField(("ids"), primary_key=True)
def __str__(self):
return self.dealersName
class DealershipListing(models.Model):
uniqueID = models.IntegerField(("CarID"), primary_key=True)
vincode = models.CharField(('vinCode'), max_length=255)
price = models.CharField(('price'), max_length=30)
msrp = models.CharField(('msrp'), max_length=30)
mileage = models.CharField(('mileage'), max_length=9)
is_new = models.BooleanField(('isNew'))
first_seen = models.CharField(("first_seen"), max_length=15)
last_seen = models.CharField(("last_seen"), max_length=15)
model = models.CharField(("Models"), max_length= 255)
make = models.CharField(("Make"), max_length=255)
year = models.CharField(("Year"), max_length= 4)
ids = models.ForeignKey(Dealer, on_delete=CASCADE)
color = models.CharField(("ExtColor"), max_length=255, null=True, blank = True)
intcolor = models.CharField(("IntColor"), max_length=255, null=True, blank=True)
def __str__(self):
return self.year + " " + self.make + " " + self.model
But when I go check the admin database, the DealershipListing is a dropdown of the names of every dealership in the Dealer model, but the ids on the DealershipListing model is placed in the place of color. What am I doing wrong? How do I connect each car in the DealershipListing to their Dealership by the ids?
I have two models with many to many relationships with custom table such as Clinic and Doctor and custom table DoctorClinic.
how could I get the list of Doctors based on Clinics?
this is my views.py
class ClinicDetailView(generic.DetailView):
model = Clinic
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['doctor_list'] = self.doctor_set.annotate(
shift=F('doctor_list__shift'),
timming=F('doctor_list__timming')
return context
I got the error object has no attribute doctor_set
here is my models.py
class Clinic(models.Model):
name = models.CharField(max_length = 256)
area = models.ForeignKey(Area, on_delete=models.CASCADE,default=None,null=True)
city = models.ForeignKey(City, on_delete=models.CASCADE,default=None,null=True)
address = models.TextField()
contact = models.CharField(max_length = 18)
category = models.CharField(max_length=30,choices = CHTYPE)
lat = models.FloatField()
lon = models.FloatField()
class Doctor(models.Model):
name = models.CharField(max_length = 256)
speciality = models.CharField(max_length = 256)
contact = models.CharField(max_length = 12)
speciality = models.ForeignKey(Speciality, on_delete=models.CASCADE)
clinic_hospital = models.ManyToManyField(Clinic, through='DoctorHospital')
class DoctorHospital(models.Model):
clinic = models.ForeignKey(ClinicHospital, on_delete=models.CASCADE)
doctor = models.ForeignKey(Doctor, on_delete=models.CASCADE)
shift = models.CharField(max_length=10)
# time_from = models.DateTimeField(auto_now_add=False,blank=True)
# time_to = models.DateTimeField( auto_now_add=False,blank=True)
timing = models.CharField(max_length=20,blank=True)
and here is my urls.py
path('clinichospital/<int:pk>/',views.ClinicHospitalDetailView.as_view(),name = 'clinichospital_detail')
please help me how can doctor list I show.
Try this out:
class ClinicDetailView(generic.DetailView):
model = Clinic
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
client_id = self.kwargs['pk'] # Pull id out of url
client = Client.objects.get(id=client_id)
context['doctor_list'] = client.doctor_set.annotate(
shift=F('doctor_list__shift'),
timming=F('doctor_list__timming')
) # Missing bracket
return context
I have models.py
class employees(models.Model):
emp_id=models.PositiveIntegerField()
emp_name = models.CharField(max_length = 100)
emp_lname = models.CharField(max_length = 100)
emp_loc=models.CharField(max_length=5,choices=LOCATION)
manager_id=models.ForeignKey('self',null=True,blank=True)
class leave(models.Model):
employee = models.ForeignKey(employees, on_delete=models.CASCADE, default='1')
start_date = models.DateField()
end_date = models.DateField()
status=models.CharField(max_length=1,choices=LEAVE_STATUS,default='P')
ltype=models.CharField(max_length=2,choices=LEAVE_TYPE)
class notify(models.Model):
sender_id=models.ForeignKey(leave, related_name='%(class)s_sendername')
receiver_id=models.ForeignKey(leave,related_name='%(class)s_receivername')
date_time=models.DateTimeField(auto_now_add=True)
viewed=models.CharField(max_length=2)
I want the employee id of receiver_id as receiver_id is a foreign key...
When I query
notify.objects.filter(receiver_id__employee__emp_id=1)
I am getting empty queryset but I want the tuples with emp_id=1.
here notify.objects.filter(receiver_id__employee__emp_id=1) Make sure you have assigned emp_id field as it's not auto incremented field.
also try
notify.objects.filter(receiver_id__employee__id=1)
Can you see why I am getting this model validation error?
I have the table in MySQL called 'paypal_transactions' with records.
I'm trying to copy this project over to another computer with the existing database.
Error Message:
One or more models did not validate: store.purchase: 'paypal_transaction' has a
relation with model <class 'f3.paypal.models.PaypalPaymentTransactions'>, which
has either not been installed or is abstract.
paypal/models.py
class PaypalPaymentTransactions(models.Model):
class Meta:
db_table = 'paypal_transactions'
payment_id = models.CharField(max_length = 50)
payer = models.CharField(max_length = 25)
amount = models.DecimalField(decimal_places = 2, max_digits = 8,
blank = True, default = "0.00")
currency = models.CharField(max_length = 10)
store/models.py
from f3.paypal.models import PaypalPaymentTransactions
class Purchase(models.Model):
user = models.ForeignKey(User, related_name = 'purchase_user')
product = models.ForeignKey(Design)
quantity = models.IntegerField()
paypal_transaction = models.ForeignKey(
PaypalPaymentTransactions,
default = None,
null = True,
blank = True)
This error occurs probably because of a dependency issue:
Try use the ForeignKey like this:
class Purchase(models.Model):
user = models.ForeignKey(User, related_name = 'purchase_user')
product = models.ForeignKey(Design)
quantity = models.IntegerField()
paypal_transaction = models.ForeignKey(
'f3.paypal.PaypalPaymentTransactions',
default = None,
null = True,
blank = True)