Django custom models.Manager queryset field undefined


I’m implementing a soft delete for class Animal. Following the example in the docs I created a custom manager for it, but the query field, ‘Inactive_Date’ is undefined. I tried putting the AnimalManager class def inside the Animal class def; no help.

Code from

class AnimalManager(models.Manager):
    def get_queryset(self):
        return super().get_queryset().filter(Inactive_Date == None)
class Animal(models.Model):
    Name = models.CharField(max_length=64, unique=True)
    Inactive_Date = models.DateField(null=True, blank=True)
    Animal_Type = models.ForeignKey(Animal_Type, null=True, on_delete=models.SET_NULL, default=None)
    Comments = models.CharField(max_length=255, blank=True)
    def __str__(self) -> str:
        return (self.Name)
    def delete(self):
        self.Inactive_Date =
    objects = AnimalManager() # omits inactive animals


Seems like you have double equal sign on the filtering

return super().get_queryset().filter(Inactive_Date == None)

But regardless, checking for NULL value can be done via __isnull

return super().get_queryset().filter(Inactive_Date__isnull=True)

Answered By – Niel Godfrey Ponciano

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published