Django:检查数据库中是否已经存在更新的字段

Django:检查数据库中是否已经存在更新的字段,django,validation,django-forms,Django,Validation,Django Forms,我正试图用email作为一个字段来更新django模型表单。在create表单中,我检查该字段是否已经存在于数据库中,如果已经存在,则引发验证错误。这部分工作正常 但是,如果我使用相同的签入更新表单,它会抛出验证错误,因为现有记录包含给定的电子邮件。是否有直接的方法来确保此验证 forms.py class MyObjCreateForm(forms.ModelForm): first_name = forms.CharField(max_length=50, label='First

我正试图用email作为一个字段来更新django模型表单。在create表单中,我检查该字段是否已经存在于数据库中,如果已经存在,则引发验证错误。这部分工作正常

但是,如果我使用相同的签入更新表单,它会抛出验证错误,因为现有记录包含给定的电子邮件。是否有直接的方法来确保此验证

forms.py

class MyObjCreateForm(forms.ModelForm):
    first_name = forms.CharField(max_length=50, label='First name')
    last_name = forms.CharField(max_length=50, label='Last name')
    email = forms.EmailField(label='Email')
    location = forms.ChoiceField(choices=TP_TM_Location, label='Location')
    designation = forms.ChoiceField(choices=TP_TM_Designation, label='Designation')
    date_of_joining = forms.DateField(label='Date of joining',
                                    widget=forms.TextInput(attrs={'type': 'date'}),
                                    initial=date.today())
    username = forms.CharField(label='Login username')
    password = forms.CharField(widget=forms.PasswordInput(), label='Login password')
    current_status = forms.ChoiceField(choices=TP_Status, label='Current status')

    def clean_email(self):
        email = self.cleaned_data.get('email')
        try:
            match = MyObj.objects.get(email=email)
            raise forms.ValidationError("email already exists in system. Please check the existing list.")
        except MyObj.DoesNotExist:
            return email

class MyObjUpdateForm(forms.ModelForm):
    first_name = forms.CharField(max_length=50, label='First name')
    last_name = forms.CharField(max_length=50, label='Last name')
    email = forms.EmailField(label='Email')
    location = forms.ChoiceField(choices=TP_TM_Location, label='Location')
    designation = forms.ChoiceField(choices=TP_TM_Designation, label='Designation')
    date_of_joining = forms.DateField(label='Date of joining',
                                    widget=forms.TextInput(attrs={'type': 'date'}),
                                    initial=date.today())
    current_status = forms.ChoiceField(choices=TP_Status, label='Current status')
    username = forms.CharField(label='Login username')

    def clean_email(self):
        email = self.cleaned_data.get('email')
        try:
            match = MyObj.objects.get(email=email)
            raise forms.ValidationError("email already exists in system. Please check the existing list.")
        except MyObj.DoesNotExist:
            return email

谢谢,

为此,您需要像这样从查询集中排除当前用户

    def clean_email(self):
        email = self.cleaned_data.get('email')
        email_match = MyObj.objects.filter(email=email).exclude(pk=self.instance.pk)  
        if self.instance and self.instance.pk and not email_match:
            return email
        else:
            raise forms.ValidationError("email already exists in system. Please check the existing list.")  

您需要删除更新类中的验证。您只需返回电子邮件,无需验证

def clean_email(self):
    return self.cleaned_data.get('email')
但是,如果要检查用户是否已收到新电子邮件,则可以使用以下验证方法:

def clean_email(self):
    email = self.cleaned_data.get('email')
    if self.instance.email == email:
        raise forms.ValidationError("Email already present.")
    return email