Python 3.8.4
Django версия 3.0.8
Привет, ребята, я начал веб-программирование CS50 от HarvardX с Python и JavaScript.
Следуя проекту там, в лекции 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
Вот мой model.py
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}"
Нет поля с именем «рейс». Я не понимаю.
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, но ни один из них не работал. Если у кого-то будет ошибка, помогите пожалуйста.