Home > Blockchain >  Django custom models.Manager queryset field undefined
Django custom models.Manager queryset field undefined

Time:09-28

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 models.py:

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 = datetime.datetime.today()
        self.save()
    objects = AnimalManager() # omits inactive animals

CodePudding user response:

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)
  • Related