3

I have two models as follows :

class FlightSchedule(models.Model):
    tail_number = models.ForeignKey(TailNumber, null=False, blank=False)
    flight_number = models.CharField(max_length=30, null=False, blank=False)
    flight_group_code = models.ForeignKey(FlightGroup, null=False, blank=False)
    origin_port_code = models.ForeignKey(Port, null=False, related_name="Origin", blank=False)
    destination_port_code = models.ForeignKey(Port, null=False, related_name="Destination", blank=False)
    flight_departure_time = models.TimeField()
    start_date = models.DateField()
    end_date = models.DateField()

    def __unicode__(self):
        return u'%s' % self.flight_number

    class Meta:
        verbose_name_plural = "flights Schedule"


class PosFlightSchedule(models.Model):
    tail_number = models.ForeignKey(TailNumber, null=False, blank=False)
    pos_flight_number = models.ForeignKey(FlightSchedule, max_length=30, null=False, blank=False,
                                      related_name='pos_flight_number')
    pos_flight_departure_time = models.ForeignKey(FlightSchedule, max_length=30,
                                              related_name='pos_flight_departure_time')
    pos_route_id = models.ForeignKey(FlightScheduleDetail, null=False, blank=False, related_name='pos_route_id')
    pos_flight_date = models.ForeignKey(FlightScheduleDetail, null=False, blank=False, related_name='pos_flight_date')
    pax_count = models.IntegerField(null=True)

    def __unicode__(self):
        return u'%s' % self.pos_flight_number

    class Meta:
        verbose_name_plural = "Flights Schedule"

For the pos_flight_departure_time , I need the choices from flight_departure_time from the FlightSchedule class. But I get the flight_number values in the drop down. What do I have to change, to get the flight_departure_time values? The classes are from different apps in a single django project. So they have two admin files.

Sahana Prabhakar
  • 581
  • 1
  • 8
  • 21
  • Possible duplicate of [Django admin - change ForeignKey display text](http://stackoverflow.com/questions/6836740/django-admin-change-foreignkey-display-text) – AKS May 11 '17 at 04:25

2 Answers2

2

No you don't actually need that. You need only one foreign key in your second model to FlightScheduleDetail and you need just one foreign key to FlightSchedule

class PosFlightSchedule(models.Model):
    tail_number = models.ForeignKey(TailNumber, null=False, blank=False)
    flight = models.ForeignKey(FlightSchedule, null=False, blank=False,related_name='pos_flight_number')
                                              related_name='pos_flight_departure_time')
    pos_route_id = models.ForeignKey(FlightScheduleDetail, null=False, blank=False, related_name='pos_route_id')
            pax_count = models.IntegerField(null=True)

    def __unicode__(self):
        return u'%s' % self.pos_flight_number

    class Meta:
        verbose_name_plural = "Flights Schedule"

Then all the fields declared in the first model automatically become available to PosFlightSchedule

So for example you can do

p = PosFlightSchedule.objects.all()[0]
print (p.flight.flight_number)
print (p.flight.pos_flight_departure_time)

etc.

This is the correct way to do it.

e4c5
  • 52,766
  • 11
  • 101
  • 134
0

You may solve this by defining model form, and by not changing to models.py

class PosFlightScheduleForm(forms.ModelForm):
    pos_flight_departure_time = forms.ChoiceField(label="Pos Department Time",
                                   choices=[(i.pk, i.flight_departure_time) for i in FlightSchedule.objects.all()],
                                   required=True)


    def __init__(self, *args, **kwargs):
        super(PosFlightScheduleForm, self).__init__(*args, **kwargs)
        self.fields['pos_flight_departure_time'] = forms.ChoiceField(label="Pos Department Time",
                                   choices=[(i.pk, i.flight_departure_time) for i in FlightSchedule.objects.all()],
                                   required=False)


    class Meta:
        model = PosFlightSchedule
        fields = (
            "tail_number", 'pos_flight_departure_time',)

In view.py You may use this form

def add_view(self, request):
    form = PosFlightScheduleForm(request.POST or None)

    if form.is_valid():
        form.save()                                    
        return redirect('/postflights')
    context = {
        'form': form,
    }
    return render(request, 'path/form.html', context)
Rahil
  • 632
  • 6
  • 17