Django UpdateView 错过了查询集

我想不通,我的代码有什么问题。我尝试了很多东西,我的 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']



白衣染霜花
浏览 140回答 1
1回答

江户川乱折腾

这是回溯需要注意的部分:Define GateHandlingUpdate.model, GateHandlingUpdate.queryset, or override GateHandlingUpdate.get_queryset().在这种情况下,第一个建议是最简单的。只是设置model = GateHandling在视图上。class GateHandlingUpdate(UpdateView):&nbsp; &nbsp; model = GateHandling&nbsp; &nbsp; form_class = GateHandlingUpdateForm&nbsp; &nbsp; template_name = 'management/gatehandling_update.html'
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python