Django ORM 更好的方法来操作 Django ORM 查询
Django ORM better way to manipulate django ORM query
型号如下:
class Seat(models.Model):
hall = models.ForeignKey(Hall,on_delete=CASCADE)
type = models.TextField(verbose_name="Seat Type")
class Show(models.Model):
show_time = models.TimeField(verbose_name='Show Time')
movie = models.ForeignKey(Movie,on_delete=CASCADE)
hall = models.ForeignKey(Hall,on_delete=CASCADE)
cinema = models.ForeignKey(Cinema,on_delete=CASCADE)
class Booking(models.Model):
seat = models.ForeignKey(Seat,on_delete=CASCADE)
show = models.ForeignKey(Show,on_delete=CASCADE)
movie = models.ForeignKey(Movie,on_delete=CASCADE)
hall = models.ForeignKey(Hall,on_delete=CASCADE)
cinema = models.ForeignKey(Cinema,on_delete=CASCADE)
user = models.ForeignKey(User, verbose_name="Username", on_delete=DO_NOTHING) # donothing
型号说明:
Multiple Seats in a Hall
Each Hall hosts multiple Shows
Each Booking is basically a ticket for a show with a specific seat and a specific show.
要求是获取一个查询集,其中包含特定演出的预订 table 中不存在的所有座位。
基本上,通过检查 Bookings table
中是否存在可用座位列表来获取节目的可用座位列表
SQL 查询如下所示:
SELECT * FROM Seat as S join Show as Sh on S.hall = Sh.hall join Bookings as B on B.show = Sh.id where Sh.id = 1 AND B.seat IS NULL
如何将其转换为 Django ORM:
可以这样做(有没有比创建 seat_id_list 更好的方法?):
qs = Seat.objects.filter(hall=hid) #----------------------------------
show_bookings = Booking.objects.filter(show=spk)
seat_id_list = []
for each_booking in show_bookings:
print(each_booking.seat.id)
seat_id_list.append(each_booking.seat.id)
qss = Seat.objects.exclude(id__in = seat_id_list)
您可以通过以下方式获得这些 Seat
:
Seat.objects.filter(<strong>hall=hid</strong>).exclude(<strong>booking__show=spk</strong>)
这将检索给定 Hall
对象 hid
的所有 Seat
,其中 Show
没有 Booking
由 spk
对象。
型号如下:
class Seat(models.Model):
hall = models.ForeignKey(Hall,on_delete=CASCADE)
type = models.TextField(verbose_name="Seat Type")
class Show(models.Model):
show_time = models.TimeField(verbose_name='Show Time')
movie = models.ForeignKey(Movie,on_delete=CASCADE)
hall = models.ForeignKey(Hall,on_delete=CASCADE)
cinema = models.ForeignKey(Cinema,on_delete=CASCADE)
class Booking(models.Model):
seat = models.ForeignKey(Seat,on_delete=CASCADE)
show = models.ForeignKey(Show,on_delete=CASCADE)
movie = models.ForeignKey(Movie,on_delete=CASCADE)
hall = models.ForeignKey(Hall,on_delete=CASCADE)
cinema = models.ForeignKey(Cinema,on_delete=CASCADE)
user = models.ForeignKey(User, verbose_name="Username", on_delete=DO_NOTHING) # donothing
型号说明:
Multiple Seats in a Hall
Each Hall hosts multiple Shows
Each Booking is basically a ticket for a show with a specific seat and a specific show.
要求是获取一个查询集,其中包含特定演出的预订 table 中不存在的所有座位。 基本上,通过检查 Bookings table
中是否存在可用座位列表来获取节目的可用座位列表SQL 查询如下所示:
SELECT * FROM Seat as S join Show as Sh on S.hall = Sh.hall join Bookings as B on B.show = Sh.id where Sh.id = 1 AND B.seat IS NULL
如何将其转换为 Django ORM:
可以这样做(有没有比创建 seat_id_list 更好的方法?):
qs = Seat.objects.filter(hall=hid) #----------------------------------
show_bookings = Booking.objects.filter(show=spk)
seat_id_list = []
for each_booking in show_bookings:
print(each_booking.seat.id)
seat_id_list.append(each_booking.seat.id)
qss = Seat.objects.exclude(id__in = seat_id_list)
您可以通过以下方式获得这些 Seat
:
Seat.objects.filter(<strong>hall=hid</strong>).exclude(<strong>booking__show=spk</strong>)
这将检索给定 Hall
对象 hid
的所有 Seat
,其中 Show
没有 Booking
由 spk
对象。