I am building simple form which allows user to change his basic (first and last name, email) data. I want to be sure that:
- emails still unique across database
- user can leave his email untouched
- user can change his email
I wanted to use ModelForm for this. I've finished with something like:
class UserDataForm(ModelForm):
class Meta:
model = User
fields = ['first_name', 'last_name', 'email']
def clean_email(self):
cd = self.cleaned_data
email = cd['email']
# Not sure how to check is there is an other account which uses this email EXCEPT this particular user account
I need to show validation error message when there is another account which uses same email AND this account isn't owned by user who is filling the form.
I don't know how to achieve this.