python - Django:重写抽象模型的 save() 函数

标签 python django django-models

我尝试重写抽象模型上的 save() 函数,但收到错误

Manager isn't accessible via Entry instances

因此,如果可能的话,如何重写抽象模型上的保存函数。由此扩展的模型是 Entry

这是我的模型代码:

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

(对于熟悉的人来说,我正在扩展 zinnia-blog 上的 Entry 模型,但认为这不相关)

最佳答案

self.__class__().objects 应该是 self.__class__.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.

关于python - Django:重写抽象模型的 save() 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5161805/

相关文章:

python - 最大 Beaglebone Black UART 波特率?

python - 基本 django-rest-framework : how to get started writing a view?

database - 没有数据库的django模型

python - Django 模型对空白值进行验证

python - 在 Python 中安装包时 pip install tr​​aceback 错误

python - 从硬币和数量创建平面列表

python - 如何计算一个值重复最少次数的次数

python - Django 1.6,如何为CreateView设置字段默认值

django - 如何将EC2中的Django连接到RDS中的Postgres数据库?

Django,用名字和姓氏注册用户?