Abstract Inheritance in Django Model, causing MAX recursion depth error

亡梦爱人 提交于 2020-01-03 08:53:09

问题


I'm trying to implement abstract inheritance in Django with the following code, but it produces a MAX recursion depth error. I'm trying to override a model's save method.

class BaseModel(models.Model):
    class Meta:
        abstract = True

    def save(self, *args, **kwargs):
        #i'm doing something here

        #i think the problem is in the return statement specifically because of the
        #self.__class__ expression.
        return super(self.__class__, self).save(*args, **kwargs)

class MyModel(BaseModel):
    p = models.CharField(max_length=30)

produces this error (end of the trace, it's lengthy):

  File "/home/jultra/ap3w/jultra_01/mysite/testsite/models.py", line 10, in save
    return super(self.__class__, self).save(*args, **kwargs)
  File "/home/jultra/ap3w/jultra_01/mysite/testsite/models.py", line 10, in save
    return super(self.__class__, self).save(*args, **kwargs)
  File "/home/jultra/ap3w/jultra_01/mysite/testsite/models.py", line 10, in save
    return super(self.__class__, self).save(*args, **kwargs)
  File "/home/jultra/ap3w/jultra_01/mysite/testsite/models.py", line 10, in save
    return super(self.__class__, self).save(*args, **kwargs)
RuntimeError: maximum recursion depth exceeded

回答1:


Do not call super on self.__class__! Call it on the actual class:

return super(BaseModel, self).save(*args, **kwargs)

This is because self.__class__ always refers to the actual concrete class of the instance. So if you inherit MyModel from BaseModel, when you get to the save method in BaseModel self.__class__ is still MyModel. So it finds the super of MyModel, which is BaseModel, so calls the save in BaseModel, which once again finds the super of MyModel...



来源:https://stackoverflow.com/questions/10948132/abstract-inheritance-in-django-model-causing-max-recursion-depth-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!