相关疑难解决方法(0)

Python类装饰器扩展类导致递归

我正在覆盖a的save方法,ModelForm我不知道它为什么会导致递归:

@parsleyfy
class AccountForm(forms.ModelForm):
    def save(self, *args, **kwargs):
        # some other code...
        return super(AccountForm, self).save(*args,**kwargs)
Run Code Online (Sandbox Code Playgroud)

导致这个:

maximum recursion depth exceeded while calling a Python object
Run Code Online (Sandbox Code Playgroud)

Stacktrace显示此行反复调用自身:

return super(AccountForm, self).save(*args,**kwargs) 
Run Code Online (Sandbox Code Playgroud)

现在,欧芹装饰器是这样的:

def parsleyfy(klass):
    class ParsleyClass(klass):
      # some code here to add more stuff to the class
    return ParsleyClass
Run Code Online (Sandbox Code Playgroud)

正如@DanielRoseman所说的那样,Parsley装饰器扩展了AccountForm导致它super(AccountForm,self)不断调用自己,解决方案是什么?

此外,我无法理解为什么这会导致递归.

python forms django recursion save

5
推荐指数
1
解决办法
969
查看次数

标签 统计

django ×1

forms ×1

python ×1

recursion ×1

save ×1