django.db.utils.IntegrityError: UNIQUE constraint failed: authentication_customusermodel.username

I was getting a unique constrained issue with my custom user model
It always save a user with an empty username so when I created a new user I got the error
I am tryin to automatically set the email of the user to be the username

models/users.py


class CustomUserModel(AbstractUser):

    """
    Custom user model that sets the username to be the same as the email address.

    This model extends the built-in AbstractUser model and ensures that the username field
    is set to the same value as the email field.

    Fields:
    - email: The unique email address of the user.
    - username: The username of the user (set to the email address).

    Note: It's important to use this model with the provided signal receiver to enforce
    setting the username to the email address.
    """
    objects = CustomUserManager()

    email = models.EmailField(unique=True)

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = []

    def is_landlord(self):
        return hasattr(self, 'landlordprofile')

    def is_tenant(self):
        return hasattr(self, 'tenantprofile')

    def is_vendor(self):
        return hasattr(self, 'vendorprofile')

signals…py

@receiver(pre_save, sender=User)
def set_username_to_email(sender, instance, **kwargs):
    """
    Signal receiver that sets the username to be the same as the email address before saving.

    Args:
    - sender: The sender of the signal (CustomUserModel in this case).
    - instance: The instance of CustomUserModel being saved.

    This signal receiver ensures that the username field of a CustomUserModel instance is
    set to the same value as the email field before saving it to the database.
    """

    if not instance.username:
        print('Signal is working properly')
        instance.username = instance.email

customUsermanager.py

@receiver(pre_save, sender=User)
def set_username_to_email(sender, instance, **kwargs):
    """
    Signal receiver that sets the username to be the same as the email address before saving.

    Args:
    - sender: The sender of the signal (CustomUserModel in this case).
    - instance: The instance of CustomUserModel being saved.

    This signal receiver ensures that the username field of a CustomUserModel instance is
    set to the same value as the email field before saving it to the database.
    """

    if not instance.username:
        print('Signal is working properly')
        instance.username = instance.email

Solved by just adding


    def save(self, *args, **kwargs):
        if not self.pk:
            self.username = self.email
        super().save(*args, **kwargs)