Django UpdateView 错过查询集

Posted

技术标签:

【中文标题】Django UpdateView 错过查询集【英文标题】:Django UpdateView misses queryset 【发布时间】:2019-05-10 13:42:20 【问题描述】:

我不知道我的代码有什么问题。我尝试了很多东西,我的 createview 正在工作。但是在那里我使用航班而不是网关处理作为 pk。

对我来说这似乎没问题,但我不明白为什么控制台告诉我缺少查询集。

models.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)

urls.py

path('gate-handling/<int:pk>/update', views.GateHandlingUpdate.as_view(), name='gate_handling_update'),

detail.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 %

views.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

forms.py

class GateHandlingUpdateForm(ModelForm):
    class Meta:
        model = GateHandling
        fields = ['gate', 'flight']

    def __init__(self, *args, **kwargs):
        airport = kwargs.pop('airport')
        flight = kwargs.pop('flight')
        super().__init__(*args, **kwargs)
        self.fields['flight'].queryset = Flight.objects.filter(pk=flight.pk)
        self.fields['gate'].queryset = Gate.objects.filter(airport=airport)

控制台

Internal Server Error: /gate-handling/9/update
Traceback (most recent call last):
  File "D:\airport\venv\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "D:\airport\venv\lib\site-packages\django\core\handlers\base.py", line 126, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "D:\airport\venv\lib\site-packages\django\core\handlers\base.py", line 124, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "D:\airport\venv\lib\site-packages\django\views\generic\base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "D:\airport\venv\lib\site-packages\django\views\generic\base.py", line 88, in dispatch
    return handler(request, *args, **kwargs)
  File "D:\airport\venv\lib\site-packages\django\views\generic\edit.py", line 189, in get
    self.object = self.get_object()
  File "D:\airport\venv\lib\site-packages\django\views\generic\detail.py", line 30, in get_object
    queryset = self.get_queryset()
  File "D:\airport\venv\lib\site-packages\django\views\generic\detail.py", line 73, in get_queryset
    'cls': self.__class__.__name__
django.core.exceptions.ImproperlyConfigured: GateHandlingUpdate is missing a QuerySet. Define GateHandlingUpdate.model, GateHandlingUpdate.queryset, or override GateHandlingUpdate.get_queryset().
[09/Dec/2018 13:35:45] "GET /gate-handling/9/update HTTP/1.1" 500 82521
Not Found: /favicon.ico
[09/Dec/2018 13:35:45] "GET /favicon.ico HTTP/1.1" 404 2965

【问题讨论】:

请注意,如果flight 字段只有一个选项,那么将其排除在表单之外会更简单,使用fields = ['gate'] 【参考方案1】:

这是traceback需要注意的部分:

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'

【讨论】:

我已经在我的表单中声明了它。 class Meta: model=GateHandling 因此我没有在视图中双重声明它。但现在可以了!

以上是关于Django UpdateView 错过查询集的主要内容,如果未能解决你的问题,请参考以下文章

Django 1.8 UpdateView 生成 405 错误和空白模板

如何在 Django 中将小部件添加到 UpdateView

Django:仅更新UpdateView中已更改的字段

进阶剖析django通用类UpdateView(2张图)

使用基于类的 UpdateView 在 Django 中更新用户模型

Django 中 UpdateView 上的日期格式更改