Include other field as choices to foreign key, Django(包括其他字段作为外键的选择,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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:包括其他字段作为外键的选择,Django


- 我如何透明地重定向一个Python导入? 2022-01-01
- CTR 中的 AES 如何用于 Python 和 PyCrypto? 2022-01-01
- 如何使用PYSPARK从Spark获得批次行 2022-01-01
- 我如何卸载 PyTorch? 2022-01-01
- 使用 Cython 将 Python 链接到共享库 2022-01-01
- YouTube API v3 返回截断的观看记录 2022-01-01
- 使用公司代理使Python3.x Slack(松弛客户端) 2022-01-01
- 检查具有纬度和经度的地理点是否在 shapefile 中 2022-01-01
- 计算测试数量的Python单元测试 2022-01-01
- ";find_element_by_name(';name';)";和&QOOT;FIND_ELEMENT(BY NAME,';NAME';)";之间有什么区别? 2022-01-01