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
Do not call
superonself.__class__! Call it on the actual class:This is because
self.__class__always refers to the actual concrete class of the instance. So if you inheritMyModelfromBaseModel, when you get to thesavemethod inBaseModelself.__class__is stillMyModel. So it finds the super of MyModel, which is BaseModel, so calls the save in BaseModel, which once again finds the super of MyModel…