Django.core.exceptions.fielderror:无法将关键字“flight”解析为字段。选择是:第一,航班,身份证,最后

2024-10-02 08:27:21 发布

您现在位置:Python中文网/ 问答频道 /正文

Python 3.8.4

Django版本3.0.8

嘿,伙计们, 我已经用Python和JavaScript开始了HarvardX的CS50的Web编程

在那里的一个项目之后,在第4课:SQL、模型和迁移中,我一步一步地发现了这个错误

>     Watching for file changes with StatReloader
>     Performing system checks...
>     
>     System check identified no issues (0 silenced).
>     August 03, 2020 - 16:29:32
>     Django version 3.0.8, using settings 'airline.settings'
>     Starting development server at http://127.0.0.1:8000/
>     Quit the server with CTRL-BREAK.
>     [03/Aug/2020 16:29:59] "GET /flights/ HTTP/1.1" 200 449
>     Internal Server Error: /flights/1
>     Traceback (most recent call last):
>       File "C:\Users\Parth Suthar\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\handlers\exception.py",
> line 34, in inner
>         response = get_response(request)
>       File "C:\Users\Parth Suthar\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\handlers\base.py",
> line 115, in _get_response
>         response = self.process_exception_by_middleware(e, request)
>       File "C:\Users\Parth Suthar\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\handlers\base.py",
> line 113, in _get_response
>         response = wrapped_callback(request, *callback_args, **callback_kwargs)
>       File "C:\Users\Parth Suthar\Desktop\airline\flights\views.py", line 17, in flight
>         "non_passengers": Passenger.objects.exclude(flight=flight).all()
>       File "C:\Users\Parth Suthar\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\manager.py",
> line 82, in manager_method
>         return getattr(self.get_queryset(), name)(*args, **kwargs)
>       File "C:\Users\Parth Suthar\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\query.py",
> line 912, in exclude
>         return self._filter_or_exclude(True, *args, **kwargs)
>       File "C:\Users\Parth Suthar\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\query.py",
> line 921, in _filter_or_exclude
>         clone.query.add_q(~Q(*args, **kwargs))
>       File "C:\Users\Parth Suthar\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\sql\query.py",
> line 1354, in add_q
>         clause, _ = self._add_q(q_object, self.used_aliases)
>       File "C:\Users\Parth Suthar\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\sql\query.py",
> line 1381, in _add_q
>         child_clause, needed_inner = self.build_filter(
>       File "C:\Users\Parth Suthar\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\sql\query.py",
> line 1254, in build_filter
>         lookups, parts, reffed_expression = self.solve_lookup_type(arg)
>       File "C:\Users\Parth Suthar\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\sql\query.py",
> line 1088, in solve_lookup_type
>         _, field, _, lookup_parts = self.names_to_path(lookup_splitted, self.get_meta())
>       File "C:\Users\Parth Suthar\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\sql\query.py",
> line 1486, in names_to_path
>         raise FieldError("Cannot resolve keyword '%s' into field. "
>     django.core.exceptions.FieldError: Cannot resolve keyword 'flight' into field. Choices are: first, flights, id, last

这是我的模特

from django.db import models

class Airport(models.Model):
    code = models.CharField(max_length=3)
    city = models.CharField(max_length=64)

    def __str__(self):
        return f"{self.city} {self.code}"

class Flight(models.Model):
    origin = models.ForeignKey(Airport, on_delete=models.CASCADE, related_name="departures")
    destination = models.ForeignKey(Airport, on_delete=models.CASCADE, related_name="arrivals")
    duration = models.IntegerField()

    def __str__(self):
        return f"{self.id}: {self.origin} to {self.destination}"

class Passenger(models.Model):
    first = models.CharField(max_length=64)
    last = models.CharField(max_length=64)
    flights = models.ManyToManyField(Flight, blank=True, related_name="passenger")

    def __str__(self):
        return f"{self.first} {self.last}"

没有名为“flight”的字段。我不明白

views.py

from django.shortcuts import render
from .models import Flight, Passenger
from django.http import HttpResponseRedirect
from django.urls import reverse

# Create your views here.
def index(request):
    return render(request, "flights/index.html", {
        "flights": Flight.objects.all()
    })

def flight(request, flight_id):
    flight = Flight.objects.get(id=flight_id)
    return render(request, "flights/flight.html", {
    "flight": flight,
    "passengers": flight.passenger.all(),
    "non_passengers": Passenger.objects.exclude(flight=flight).all()
    })

def book(request, fight_id):
    if request.method == "POST":
        flight = Flight.objects.get(pk=flight_id)
        passenger = Passenger.objects.get(pk=int(request.POST["passenger"]))
        passenger.flights.add(flight)
        return HttpResponseRedirect(reverse("flight", args=(flight.id)))

我读过关于stackoverflow的另一个答案,但没有一个有效。 如果有人犯错,请帮忙


Tags: djangoinpyselfmodelslocallineusers
2条回答

你忘了这个

"non_passengers": Passenger.objects.exclude(flight=flight).all()

制造

飞行>&燃气轮机;航班

应该是这样的

"non_passengers": Passenger.objects.exclude(flights=flight).all()

在飞行功能中,您正在使用-

"non_passengers": Passenger.objects.exclude(flight=flight).all()

这是由于乘客没有野战航班而造成的错误。换成

"non_passengers": Passenger.objects.exclude(flights=flight).all()

它应该会起作用

相关问题 更多 >

    热门问题