I have a Django app using a PostgreSQL db, where the user migration has an email field with unique=True
. My User
model has:
email = models.EmailField(_('email address'), unique=True, blank=True)
I want to be able to create multiple users with blank emails, but when the email is not blank it has to be unique. Currently, when I create two users with a blank email I get this error:
duplicate key value violates unique constraint "accounts_user_email_b2644a56_uniq"
EDIT: I have added this to the save
method in the User
model:
if not self.email:
self.email = None