Django: override the save() function of an abstract model

Tags: , ,



I have tried to override the save() function on a model that is abstract, and am getting an error

Manager isn't accessible via Entry instances

So if possible, how do you override the save function on an abstract model. The model that extends from this is Entry

Here is my model Code:

class EntryBlog(EntryAbstractClass):
    groups = models.ManyToManyField(group, null=True, blank=True)    

    def save(self, *args, **kwargs):
        if self.featured:
            self.__class__().objects.all().update(featured = False)
        super(EntryBlog, self).save(*args, **kwargs)

    class Meta:
        abstract = True

(For those who are familiar, I am extending the Entry model on the zinnia-blog, but dont think that is relevant)

Answer

self.__class__().objects should be self.__class__.objects.

See the Notes on retrieving objects: http://docs.djangoproject.com/en/dev/topics/db/queries/#retrieving-objects

Managers are accessible only via model classes, rather than from model instances, to enforce a separation between “table-level” operations and “record-level” operations.



Source: stackoverflow