好的,所以我有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
答案 0 :(得分:1)
使用信号例如:
models.py
@receiver(pre_save, sender=User)
def User_pre_save(sender, **kwargs):
email = kwargs['instance'].email
username = kwargs['instance'].username
if not email: raise ValidationError("email required")
if sender.objects.filter(email=email).exclude(username=username).count(): raise ValidationError("email is already exist")
views.py
def view_name(request):
try:
.......
except ValidationError, e:
message_dict = e.update_error_dict({})
state = message_dict[NON_FIELD_ERRORS]
return render(request, 'page.html', {'state':state})
答案 1 :(得分:1)
您可以创建具有该功能的基本表单,然后让两个表单扩展该表单。
答案 2 :(得分:1)
从基础表单继承(与其他答案建议一样)或者由于表单是从您的模型派生的,因此您可以将其直接放在model cleaning中。这种方法的唯一问题是它每次都会访问数据库。只是向您展示选项。
...在进行任何唯一性检查之前,将调用模型的clean()方法。有关模型的clean()挂钩的详细信息,请参阅验证对象。
class MyModel(models.Model):
# ... your model
def clean(self):
if self.email and User.objects.filter(email=self.email).exclude(pk=self.id).count():
raise ValidationError('Email already in use.')
答案 3 :(得分:1)
扩展msc的答案,创建一个基本表单,并UserUpdateForm
和RegistrationForm
扩展您的基本表单。
class YourBaseForm(ModelForm):
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
class UserUpdateForm(YourBaseForm):
# ....add unique fields or methods here
class RegistrationForm(YourBaseForm):
# ....add unique fields or methods here
clean_email
方法现在可用于UserUpdateForm
和RegistrationForm
个对象。
有关表单继承的更多信息,请查看docs.
更新:
如果您需要更改子类中的方法,那么您可以覆盖它,但包括调用super的clean_email
方法,如此 -
UserUpdateForm(YourBaseForm):
def clean_email(self):
email = super(UserUpdateForm, self).clean_email()
if self.instance.email != email:
return self.instance.email
return email