我有一个应按日期过滤的电影模型。
qs = Movie.objects.filter(visible=True,).order_by('-hot', '-showing', 'name')
...
if self.kwargs.get('shortcut', None):
today = datetime.date.today()
shortcut = self.kwargs['shortcut']
if shortcut == 'now-showing':
qs = qs.filter(shows__starts__gte=today,)
elif shortcut == 'today':
qs = qs.filter(shows__starts__exact=today)
elif shortcut == 'coming-soon':
qs = qs.filter(coming_soon=True, coming_soon_starts__gte=today)
elif shortcut == 'tomorrow':
qs = qs.filter(shows__starts__exact=today + datetime.timedelta(days=1))
elif shortcut == 'this-weekend': #Friday - Sunday
days = 4 - today.weekday()
starts = today + datetime.timedelta(days=days)
ends = starts + datetime.timedelta(days=2)
qs = qs.filter(shows__starts__range=(starts, ends))
elif shortcut == 'tickets':
qs = qs.filter( Q(shows__venue__name__icontains='imax') | Q( shows__venue__name__icontains='anga' ) | Q( shows__venue__name__icontains='century-cinemax-junction' ) & Q(shows__new_price__gte=100) & Q( shows__venue__name__icontains='anga' ))
now-showing
,coming-soon
,this-weekend
过滤正确,但tickets
带来的电影已过期的start_date和价格。
我需要编辑它,以便检查开始日期和价格是否在那里,然后场地是否来自三个电影院。
答案 0 :(得分:1)
试试这个:
qs = qs.filter(Q(shows__new_price__gte=100), Q(shows__starts__gte=today), Q(shows__venue__name__icontains='imax') | Q(shows__venue__name__icontains='anga' ) | Q(shows__venue__name__icontains='century-cinemax-junction')
来自django docs:
如果为查找函数提供多个Q对象参数,则参数将“并”编辑在一起。