Reputation: 2386
I need to do an exclude on a django query to exclude records where both fields are blank but include if there is one or the other or both.
My model looks like
class Example(models.Model):
title = models.CharField(max_length=140, blank=True)
name = models.CharField(max_length=140, blank=True)
right now I am writing the query as
Example.objects.filter(general_query).exclude(title=u'').exclude(name=u'')
Upvotes: 0
Views: 56
Reputation: 20553
Or, simply put 2 conditions together under the same exclude()
:
Example.objects.filter(general_query).exclude(title=u'', name=u'')
Only both conditions are met will be excluded.
Upvotes: 1
Reputation: 118458
Use Q objects
https://docs.djangoproject.com/en/1.6/topics/db/queries/#complex-lookups-with-q-objects
Example.objects.filter(general_query).exclude(Q(title='') & Q(name=''))
Upvotes: 2