[Django]-Unique email constraint for django.contrib.auth.user

1👍

Checks for uniqueness occur during model validation as per the Model validation documentation. Calling save() on the model doesn’t automatically cause model validation. The admin application will be calling model validation.

Nothing prevents you from creating a User object in Python such as user=User('cust1','123','xxx@gmail.com') and then doing a user.save(). If there are uniqueness checks at the database level, you’ll likely get an exception at this point. If you want to check before doing the save, call is_valid() on the Model instance.

If you want every User object to be automatically validated before save, use the pre_save Django signal to do Model validation before the save proceeds.

Leave a comment