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


基础教程推荐
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 筛选NumPy数组 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01