我正在使用第三方应用程序,我希望重写原始模型的 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)
但是上面的代码片段不会运行。
因此我的问题是有没有办法覆盖模型的保存方法?或者,如果这是不可能的,有没有办法在创建第三方模型实例之前添加验证?
最佳答案
该问题与您的型号为 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
关于django - 重写 django 代理模型中的 save 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62655425/