[Answered ]-Problems with custom user model

2👍

You have unique=True on your avatar field in CustomUser model.
But in your custom manager you have this code:

    try:
        avatar = Avatar.objects.get(pk=uuid)
    except ObjectDoesNotExist:
        avatar = Avatar.objects.create(uuid=uuid, first_name=first_name, last_name=last_name)

If avatar with that uuid exists, you will get an avatar object and django will try to create user with this object. But since avatar is unique, you will get an IntegrityError, if user with that avatar exists in database.
You should raise custom exception in this place, if avatar object already exists, if you need unique behaviour. If not, remove unique=True.

Leave a comment