I’m building dynamic filters which I pass by GET to a queryset filter:
for k, v in request.GET.iteritems():
kwargs[str(k)] = str(v)
students = models.Student.objects.filter( **kwargs )
and it’s working for almost all the queries I’m throwing at it. However, I have a related model with a manytomany relationship, Group. So a student can be a member of many groups. I’m able to filter students who belong to a given group using the following:
‘groups__in='+str(group.id)
e.g. – //example.com/students/?groups__in=1
But I can’t figure out how to filter for students who don’t belong to any group. I’ve tried the following without success:
groups__in=None # students == []
groups__exact=None # students == []
groups__iexact=None # FAIL not that I really expected this to work
groups__isnull=True # students == []
The last version was what I was hoping to have actually work. I’m sure I could get this to work by modifying the top code to something like
if request.GET['something']:
students = models.Student.objects.exclude(groups__isnull=False)
else:
students = models.Student.objects.filter( **kwargs )
So I guess the question becomes, how can i create
students = models.Student.objects.exclude(groups__isnull=False)
using .filter()?
Maybe I don’t understand the question. But I see: