afshin
afshin

Reputation: 1833

Django - filter for empty integer field not working

This is the queryset I'm using:

model:

class Category(models.Model):
    categoryText = models.CharField(max_length=50)
    parentCat = models.ForeignKey('self',null=True,blank=True)

I am trying to query all the entries where parentCat is empty / not set.

queryset=Category.objects.all().filter(parent=null)

Obviously this is not working - what is the correct way of doing this query?

Upvotes: 4

Views: 3499

Answers (1)

bradley.ayers
bradley.ayers

Reputation: 38392

null is actually not a predefined object in Python. You want None:

queryset = Category.objects.filter(parentCat=None)

or

queryset = Category.objects.filter(parentCat__isnull=True)

These two are equivalent.

Upvotes: 8

Related Questions