7

I have the following filter:

open_slots = Opening.objects.filter(club_id=club_id, day=datetime.date.today(), reservation ='Open')

I want to create another list "closed_slots" that has all the same attributes as the above except that reservation is not equal to 'Open'. When I tried using reservation !='Open' I get an error. How do I fix this?

sharataka
  • 5,014
  • 20
  • 65
  • 125
  • For fine-grained control on the returned set, look at Django's ORM docs, specifically on using [`.filter()`](https://docs.djangoproject.com/en/dev/topics/db/queries/#retrieving-specific-objects-with-filters) – Peter Rowell May 22 '12 at 16:08
  • You can also negate a query - see this answer http://stackoverflow.com/questions/687295/how-do-i-do-a-not-equal-in-django-queryset-filtering – danbgray Jan 30 '13 at 10:34

2 Answers2

21

Use the exclude method. Details here.

open_slots = Opening.objects.filter(club_id=club_id, day=datetime.date.today()).exclude(reservation ='Open')
shanyu
  • 9,536
  • 7
  • 60
  • 68
2

I have tried with following and it's working fine.

from django.db.models import Q
open_slots = Opening.objects.filter(~Q(reservation ='Open'),club_id=club_id, day=datetime.date.today() )
Mihir
  • 95
  • 7
Piyush S. Wanare
  • 4,703
  • 6
  • 37
  • 54