我正在使用第三方应用程序,并且希望重写原始模型的save()方法以验证某些数据。
class CustomState(State):
class Meta:
proxy = True
def save(self, *args, **kwargs):
print('hellooo in save method of state')
super(State, self).save(*args, **kwargs)
但是上面的代码片段未运行。
因此,我的问题是有没有办法替代模型的save方法?或者,如果那不可能,是否有办法在创建第三方模型实例之前添加验证?
答案 0 :(得分:1)
该问题与您的模型为proxy
无关,但与您调用父super().save()
的方式有误:
class CustomState(State):
class Meta:
proxy = True
def save(self, *args, **kwargs):
print('hellooo in save method of state')
# The wrong way to call super
# super(State, self).save(*args, **kwargs)
super(CustomState, self).save(*args, **kwargs)
浏览本教程,主题为A super() Deep Dive