unable to auto-fill data during update in dependent dropdowns

So I have been following this tutorial related to dependent dropdowns which can be found here and I seem to have gotten stuck at the last part the else if statement and am unable to implement the same in my project 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()

status 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)

status 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')       

The error that I'm getting

so if anyone could help me fix this part:

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')       

I would really appreciate it

1 answer

  • answered 2019-11-08 13:50 Jordan M.

    The error says there's no park_bay_set attribute for FlightNum, because you specified related_name='fl4' for your fl_no field inside your Flight model. Also, up to this point you are in a Flight instance, so you need one more relation to reach the park bays. Your status forms.py should be like this:

    elif self.instance.pk:
                self.fields['park_bay'].querysets = self.instance.fl_no.fl4.park_bay.all().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.fl4.park_bay.all().order_by('dep_time')
    

    Here's a diagram of how your models are currently related, not sure if this is exactly what you want, you might want to take a look and see if a normalization is needed. flights_diagram