I have been struggling with a task for two days and can not find an appropriate solution, so I would be very grateful for any help. The task is as follows: when a user logs in via Yandex/Google (I use drf_social_auth2), upon saving them in the database, the value of the username field becomes the user's email address. However, when logging in via Telegram, the value of the same username field should be the user's Telegram nickname. The first part is implemented through the USERNAME_FIELD attribute and the get_username method. The question is, how can we accomplish the second part, so that the username equals the Telegram nickname?
One option I considered was using the username field from parent AbstractUser model to store the necessary value depending on the method, but there's an issue with the fact that drf_social_oauth2, which I use for authorization, saves the email as the email value, and I can't find ways to make it save email to username field.
Here is the code for my User:
class User(AbstractUser, TimeStampedUUIDModel, PermissionsMixin):
# First and last name do not cover name patterns around the globe
name = CharField(_("Name of User"), blank=True, max_length=255)
email = EmailField(_("email address"), unique=True)
USERNAME_FIELD = "email"
REQUIRED_FIELDS = []
objects = UserManager()
class Meta:
verbose_name = _("User")
verbose_name_plural = _("Users")
def __str__(self):
return f"{self.email}"
@property
def get_short_name(self):
return self.name
I would be grateful for any advice, from customizing drf_social_auth2 to make some sort of dynamic USERNAME_FIELD.