问题描述
我正在通过构建非常小规模的应用程序来练习 django。我使用 3 个表 - 机场、航班、乘客制作了一个航班预订系统。这是我的模型类-
from django.db import models
# Create your models here.
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,related_name = "arrivals")
duration = models.IntegerField()
def __str__(self):
return f"{self.id} {self.origin} to {self.destination}"
# A flight can have multiple passengers or a passenger can board on multiple flights
class Passenger(models.Model):
first = models.CharField(max_length=64)
last = models.CharField(max_length=64)
# flights establishes many to many relationship with Flight table
flights = models.ManyToManyField(Flight,blank=True,related_name="passengers")
def __str__(self):
return f"{self.first} {self.last}"
我已设法显示乘客列表并为特定乘客预订航班,但我无法从特定航班中删除乘客。问题是我还没有清楚地理解 django 模型中的多对多关系。这是我的视图函数代码片段-
from django.shortcuts import render
from django.http import HttpResponseRedirect
from django.urls import reverse
from .models import *
# 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(pk=flight_id)
#Get all the passengers who are present on the flight
passengers = flight.passengers.all()
#Returns a new QuerySet containing objects that do not match the given lookup parameters.
non_passengers = Passenger.objects.exclude(flights=flight).all() # Returns all the passenger who are not on the particular flight
return render(request,"flights/flight.html",{
"flight":flight,"passengers":passengers,"non_passengers":non_passengers,})
def book(request,flight_id):
if request.method == "POST":
#Access the the flight by specifying the primary key
flight = Flight.objects.get(pk=flight_id)
#Finding the passenger id and passenger from submitted data
passenger = Passenger.objects.get(pk=int(request.POST["passenger"]))
#Add passenger to the flight
passenger.flights.add(flight)
#Redirect the user to flight page
return HttpResponseRedirect(reverse("flight",args=(flight.id,)))
def delete(request,flight_id):
if request.method == "POST":
#Access the the flight by specifying the primary key
flight = Flight.objects.get(pk=flight_id)
#Finding the passenger id and passenger from submitted data
passenger_id = Passenger.objects.get(pk=int(request.POST["passenger"]))
print(passenger)
#Remove passenger from the flight
passenger.flight.delete(passenger_id)
#Redirect the user to flight page
return HttpResponseRedirect(reverse("flight",args=(flight.id)))
这是我用于显示预订系统的 HTML -
{% extends "flights/layout.html" %}
{% block body %}
<h1>Flight {{ flight.id }}</h1>
<ul>
<li>Origin: {{ flight.origin }}</li>
<li>Destination: {{ flight.destination }}</li>
<li>Duration: {{ flight.duration }}</li>
</ul>
<h3>Passengers</h3>
<ul>
{% for passenger in passengers %}
<li>{{passenger}}</li>
{% empty %}
<li>No passengers</li>
{% endfor %}
</ul>
<h2>Add Passenger</h2>
<form action="{% url 'book' flight.id %}" method="POST">
{% csrf_token %}
<select name="passenger" id="">
{% for passenger in non_passengers %}
<option value="{{passenger.id}}">{{ passenger }}</option>
{% endfor %}
</select>
<input type="submit">
</form>
<h2>Delete Passenger</h2>
<form action="{% url 'delete' flight.id %}" method="POST">
{% csrf_token %}
<select name="passenger" id="">
{% for passenger in passengers %}
<option value="{{passenger.id}}">{{ passenger }}</option>
{% endfor %}
</select>
<input type="submit">
</form>
<a href="{% url 'index' %}">Back to Flight List</a>
{% endblock %}
谁能向我解释 django 中的多对多关系,并建议我如何从特定航班中删除乘客(或取消他的预订)?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)