好的,我有UserUpdateForm和RegistrationForm。当前每个都具有此功能:
def clean_email(self):
email = self.cleaned_data.get('email')
if email and User.objects.filter(email=email).exclude(pk=self.instance.id).count():
raise forms.ValidationError('Email already in use.')
return email
我想知道什么是避免这种重复的理想方法。你知道吗
请告知。你知道吗
**更新**
如果我需要调用父函数,但是需要调用所有的东西,比如我有:
def clean_email(self):
email = self.cleaned_data.get('email')
if email and User.objects.filter(email=email).exclude(pk=self.instance.id).count():
raise forms.ValidationError('Email already in use.')
### THIS BIT IS ONLY NEEDED IN ONE OF THE CHILD FORMS ###
# Check whether the email was change or not
if self.instance.email != email:
# To not change the email in our database until the new one is verified
return self.instance.email
###
return email
使用信号例如:
你知道吗视图.py你知道吗
在msc的答案上展开,创建一个基窗体并让
UserUpdateForm
和RegistrationForm
扩展您的基窗体。你知道吗clean_email
方法现在在UserUpdateForm
和RegistrationForm
对象上都可用。你知道吗有关表单继承的更多信息,请浏览docs.
更新:
如果您需要更改子类中的方法,那么您可以重写它,但包括对super的
clean_email
方法的调用,如下所示-您可以创建一个具有该函数的基窗体,然后让两个窗体都扩展该函数。你知道吗
相关问题 更多 >
编程相关推荐