我有以下两种型号:
class Tour(models.Model):
name = models.CharField(max_length=100)
description = models.CharField(max_length=3000, blank=True
# some other fields here
和
class TourDatesInfo(models.Model):
departure_date = models.DateTimeField()
return_date = models.DateTimeField()
tour = models.ForeignKey(Tour, on_delete=models.CASCADE, related_name='dates')
Tour
型号与TourDatesInfo
型号之间存在一对多关系,因此一次旅行可能会有多组不同的出发/返回日期.
What I try to achieve is to be able to filter the tours QuerySet
depending on their set of TourDatesInfo
, preciesely on whether each tour contains departure/return pair that satisfies specific conditions, e.g., get all tours that have at least one TourDatesInfo
with departure_date
> 2022-04-12 和 return_date
< 2022-05-01.
我可以编写一个SQL查询来执行这个操作,大约SELECT * FROM tours_tour tours WHERE tours.id IN (SELECT DISTINCT tour_id FROM tours_tourdatesinfo WHERE departure_date > '2022-04-12' AND return_date < '2022-05-01');
个,但是如何使用django ORM而不使用原始查询呢?