抽象Django模型没有“model”属性?

2024-09-30 06:17:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我在Django应用程序中有一个抽象模型:

class HistoryTrackedModel(models.Model):
    def save(self, *args, **kwargs):
        super(self.model, self).save(*args, **kwargs) # Call the real save method
        # Do some miscellaneous work here (after saving)

    class Meta:
        abstract = True

子模型使用抽象模型作为其基础:

^{pr2}$

当我实例化一个Project实例(子模型)并调用save()方法时,我得到以下错误:

'Project' object has no attribute 'model'

它在抽象类的save方法中的super(self.model, self).save()调用失败。我试图将该方法更改为以下方法,但它(很明显,现在我看到它)陷入了递归循环中:

class HistoryTrackedModel(models.Model):
    def save(self, *args, **kwargs):
        my_model = type(self)
        super(my_model, self).save(*args, **kwargs) # Call the real save method

我做错什么了?不应该所有从基类继承的子类(基类本身继承自模型。模型)是否包含model属性?在


Tags: the方法模型selfmodelmodelssavedef

热门问题