我想不通,我的代码有什么问题。我尝试了很多东西,我的 createview 正在工作。但是在那里我使用航班而不是门把手作为 pk。
对我来说这似乎没问题,我不明白为什么控制台告诉我缺少查询集。
模型.py
class Airport(models.Model):
name = models.CharField(max_length=255, unique=True)
class Flight(models.Model):
start = models.ForeignKey(Airport, on_delete=models.CASCADE,
related_name='start')
end = models.ForeignKey(Airport, on_delete=models.CASCADE,
related_name='end')
number = models.CharField(max_length=5, default="EJT12")
class Gate(models.Model):
airport = models.ForeignKey(Airport, on_delete=models.CASCADE)
number = models.IntegerField(default=0)
class GateHandling(models.Model):
gate = models.ForeignKey(Gate, on_delete=models.CASCADE)
flight = models.ForeignKey(Flight, on_delete=models.CASCADE)
网址.py
path('gate-handling/<int:pk>/update', views.GateHandlingUpdate.as_view(), name='gate_handling_update'),
详细信息.html
{% for flight in flights_arriving %}
{% for gate_handling in flight.gatehandling_set.all %}
<p>{{gate_handling}} <a href="{% url 'management:gate_handling_update' gate_handling.pk %}">Change</a></p>
{% empty %}
<p>Gate <a href="{% url 'management:gate_handling_create' flight.pk %}">Assign</a></p>
{% endfor %}
{% endfor %}
视图.py
class GateHandlingUpdate(UpdateView):
form_class = GateHandlingUpdateForm
template_name = 'management/gatehandling_update.html'
def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
kwargs['airport'] = Gate.objects.get(gatehandling=self.object).airport
kwargs['flight'] = Flight.objects.get(pk=self.object.flight.pk)
return kwargs
表格.py
class GateHandlingUpdateForm(ModelForm):
class Meta:
model = GateHandling
fields = ['gate', 'flight']
江户川乱折腾
相关分类