简体   繁体   English

Django UpdateView错过了查询集

[英]Django UpdateView misses queryset

I can't figure out, what is wrong with my code. 我不知道代码有什么问题。 I tried so much stuff and my createview is working. 我尝试了很多东西,我的createview正在工作。 But there I use the flight and not the gatehandling as pk. 但是在这里我使用飞行而不是使用门控作为pk。

For me this seems okay, and I dont understand why the console tells me that querysets are missing. 对我来说,这似乎还可以,而且我不明白为什么控制台告诉我缺少查询集。

models.py 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 urls.py

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

detail.html 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 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)

console 安慰

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

This is the part of the traceback to pay attention to: 这是回溯中要注意的部分:

Define GateHandlingUpdate.model, GateHandlingUpdate.queryset, or override GateHandlingUpdate.get_queryset().

In this case, the first suggestion is easiest. 在这种情况下,第一个建议是最简单的。 Just set model = GateHandling on the view. 只需在视图上设置model = GateHandling

class GateHandlingUpdate(UpdateView):
    model = GateHandling
    form_class = GateHandlingUpdateForm
    template_name = 'management/gatehandling_update.html'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM