本文介绍了包括其他字段作为外键的选择,Django的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个模型如下:

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"

对于 pos_flight_departure_time ,我需要来自 FlightSchedule 类的 flight_departure_time 的选择.但我在下拉列表中得到 flight_number 值.为了获得 flight_departure_time 值,我需要改变什么?这些类来自单个 django 项目中的不同应用程序.所以他们有两个管理文件.

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.

推荐答案

不,你实际上并不需要那个.您在第二个模型中只需要一个外键来 FlightScheduleDetail 并且您只需要一个外键来 FlightSchedule

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"

然后在第一个模型中声明的所有字段自动变为可用于PosFlightSchedule

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

例如你可以这样做

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

等等.

这是正确的做法.

这篇关于包括其他字段作为外键的选择,Django的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-28 04:50