user4910881
user4910881

Reputation:

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.

Upvotes: 0

Views: 55

Answers (1)

Sander van Leeuwen
Sander van Leeuwen

Reputation: 3033

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.

Upvotes: 1

Related Questions