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.
对于pos_flight_departure_time,我需要从flight_departure_time类中选择。但我得到了下拉列表中的flight_number值。我需要改变什么,才能得到flight_departure_time值?这些类来自单个django项目中的不同应用程序。他们有两个管理文件。
2 个解决方案
#1
1
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
不,你不需要。FlightScheduleDetail只需要第二个模型中的一个外键,而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
然后,第一个模型中声明的所有字段自动成为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.
这是正确的方法。
#2
0
You may solve this by defining model form, and by not changing to models.py
您可以通过定义模型形式和不更改为model .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)
#1
1
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
不,你不需要。FlightScheduleDetail只需要第二个模型中的一个外键,而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
然后,第一个模型中声明的所有字段自动成为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.
这是正确的方法。
#2
0
You may solve this by defining model form, and by not changing to models.py
您可以通过定义模型形式和不更改为model .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)