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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With