I'm wondering how I can update a Django Users username. I am using the default auth model, with a OneToOne
relationship for a custom Profile
. The Profile
can be updated via the end user, and a signal has been set to listen to the change and update accordingly.
Even if the username is gibberish or isn't changed at all the same error occurs, so I'm not sure why the unique constraint is being violated.
# models
class Profile(models.Model):
client = models.OneToOneField('auth.User', related_name='profile')
# signals
@receiver(pre_save, sender=Profile, weak=False)
def sync_profile_auth(sender, instance, **kwargs):
if instance.pk:
instance.client.first_name = instance.first_name
instance.client.last_name = instance.last_name
instance.client.email = instance.email
instance.client.username = instance.email
instance.client.save()
# error
django.db.utils.IntegrityError: duplicate key value violates unique constraint "auth_user_username_key"
DETAIL: Key (username)=(myuser@admin.com) already exists.
Cheers.