简体   繁体   中英

Django filtering results from database

I have a movie model that's supposed to filter by date.

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' ))

on the now-showing , coming-soon , this-weekend filter correctly but tickets brings movies with expired start_date and price. I need to edit it so it checks whether the start date and price are there then whether the venue is from the three cinemas.

Try this:

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')

From the django docs :

If you provide multiple Q object arguments to a lookup function, the arguments will be “AND”ed together.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM