Итак, я следовал этому уроку, связанному с зависимыми выпадающими списками, которые можно найти здесь , и я, похоже, застрял в последней части оператора else if и не могу реализовать то же самое в моем проекте postlog models.py:
from django.db import models
# Create your models here.
class FlightNum(models.Model):
fl_no =models.CharField(max_length=5,primary_key=True)
def __str__(self):
return self.fl_no
class Destination(models.Model):
fl_no= models.OneToOneField(FlightNum,on_delete=models.CASCADE,related_name='fl1')
dest= models.CharField(max_length=15,blank=True)
def __str__(self):
return self.dest
class Parking(models.Model):
fl_no= models.OneToOneField(FlightNum,on_delete=models.CASCADE,related_name='fl2')
park_bay= models.CharField(max_length=3)
def __str__(self):
return self.park_bay
class DepartureTime(models.Model):
fl_no= models.OneToOneField(FlightNum,on_delete=models.CASCADE,related_name='fl3')
dep_time= models.CharField(max_length=9)
def __str__(self):
return self.dep_time
class Flight(models.Model):
fl_no= models.OneToOneField(FlightNum,on_delete=models.CASCADE, primary_key=True, related_name='fl4')
park_bay= models.ForeignKey(Parking, on_delete=models.CASCADE)
dest= models.ForeignKey(Destination, on_delete=models.CASCADE)
dep_time= models.ForeignKey(DepartureTime, on_delete=models.CASCADE)
inbound= models.CharField(max_length=15,blank=True)
airline= models.CharField(max_length=15)
arr_time= models.TimeField()
статус models.py:
from django.db import models
from postlog.models import FlightNum,Destination,Parking,DepartureTime
# Create your models here.
class FlightStatus(models.Model):
CLEANING_CHOICES = (
('Yes', 'Yes'),
('No','No'),
)
fl_no= models.OneToOneField(FlightNum,on_delete=models.CASCADE,related_name='fli_no',primary_key=True)
park_bay= models.ForeignKey(Parking,on_delete=models.CASCADE,related_name='parki_bay')
catering= models.CharField(max_length=9)
fuel= models.IntegerField()
pas_cnt= models.IntegerField()
dest= models.ForeignKey(Destination,on_delete=models.CASCADE,related_name='desti',null=True)
dep_time=models.ForeignKey(DepartureTime,on_delete=models.CASCADE,related_name='dept_time')
Cleaning = models.CharField( max_length=3, choices=CLEANING_CHOICES)
статус forms.py:
from django.forms import ModelForm
from status.models import FlightStatus
from postlog.models import FlightNum,Destination,Parking,DepartureTime
class StatusForm(ModelForm):
class Meta:
model = FlightStatus
fields = ('fl_no', 'park_bay', 'catering', 'fuel', 'pas_cnt', 'dest','dep_time', 'Cleaning')
labels = {
'fl_no': ('Flight Number'),
'park_bay': ('Parking Bay'),
'catering': ('Catering'),
'fuel': ('Fuel'),
'pas_cnt': ('Passenger Count'),
'dest': ('Destination'),
'dep_time': ('Departure Time'),
'Cleaning': ('Cleaning'),
}
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['park_bay'].queryset = Parking.objects.none()
self.fields['dest'].queryset = Destination.objects.none()
self.fields['dep_time'].queryset = DepartureTime.objects.none()
if 'fl_no' in self.data:
try:
flightid = int(self.data.get('fl_no'))
self.fields['park_bay'].queryset = Parking.objects.filter(fl_no=flightid).order_by('park_bay')
self.fields['dest'].queryset = Destination.objects.filter(fl_no=flightid).order_by('dest')
self.fields['dep_time'].queryset = DepartureTime.objects.filter(fl_no=flightid).order_by('dep_time')
except (ValueError, TypeError):
pass
elif self.instance.pk:
self.fields['park_bay'].querysets = self.instance.fl_no.park_bay_set.order_by('park_bay')
self.fields['dest'].querysets = self.instance.fl_no.dest_set.order_by('dest')
self.fields['dep_time'].querysets = self.instance.fl_no.park_bay_set.order_by('dep_time')
, поэтому, если кто-нибудь может помочь мне исправить эту часть:
elif self.instance.pk:
self.fields['park_bay'].querysets = self.instance.fl_no.park_bay_set.order_by('park_bay')
self.fields['dest'].querysets = self.instance.fl_no.dest_set.order_by('dest')
self.fields['dep_time'].querysets = self.instance.fl_no.dep_time_set.order_by('dep_time')
Я был бы очень признателен