【发布时间】:2024-01-14 03:49:01
【问题描述】:
我覆盖了ModelForm的save方法,不知道为什么会导致递归:
@parsleyfy
class AccountForm(forms.ModelForm):
def save(self, *args, **kwargs):
# some other code...
return super(AccountForm, self).save(*args,**kwargs)
原因:
maximum recursion depth exceeded while calling a Python object
Stacktrace 显示此行重复调用自身:
return super(AccountForm, self).save(*args,**kwargs)
现在,欧芹装饰器是这样的:
def parsleyfy(klass):
class ParsleyClass(klass):
# some code here to add more stuff to the class
return ParsleyClass
正如@DanielRoseman 建议的那样,扩展AccountForm 的Parsley 装饰器会导致super(AccountForm,self) 不断调用自己,解决方案是什么?
我也无法理解为什么这会导致递归。
【问题讨论】:
-
我不认为你应该返回父 save() 方法,只做 super(AccountForm, self).save(*args,**kwargs)
-
您确定这是您的实际代码吗?如果您在
super调用中错误地引用了超类,通常会发生这种情况 - 例如,您实际上有一个 AccountForm 的子类,并且在该覆盖的保存方法中您仍在调用super(AccountForm...)。 -
谢谢@DanielRoseman 我已经更新了问题
-
@PepperoniPizza 你应该肯定返回一个modelform超级保存,因为它应该返回一个实例
-
@JamesLin
ParsleyClass做什么?你能提供一个更完整的例子吗?
标签: python django forms recursion save