我想不通,我的代码有什么问题。我尝试了很多东西,我的 createview 正在工作。但是在那里我使用航班而不是门把手作为 pk。对我来说这似乎没问题,我不明白为什么控制台告诉我缺少查询集。模型.pyclass 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)网址.pypath('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 %}视图.pyclass 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表格.pyclass GateHandlingUpdateForm(ModelForm): class Meta: model = GateHandling fields = ['gate', 'flight']
1 回答
江户川乱折腾
TA贡献1851条经验 获得超5个赞
这是回溯需要注意的部分:
Define GateHandlingUpdate.model, GateHandlingUpdate.queryset, or override GateHandlingUpdate.get_queryset().
在这种情况下,第一个建议是最简单的。只是设置model = GateHandling在视图上。
class GateHandlingUpdate(UpdateView):
model = GateHandling
form_class = GateHandlingUpdateForm
template_name = 'management/gatehandling_update.html'
添加回答
举报
0/150
提交
取消