Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 404
afshin Avatar asked Feb 23 '23 22:02

afshin


1 Answers

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.

like image 109
bradley.ayers Avatar answered Feb 26 '23 20:02

bradley.ayers