Remove or disable username for users in Django

Is there a way to completely remove or disable a username in Django?

I want to use only email and password.

+3
source share
2 answers

You cannot disable it, but you can generate an arbitrary username placeholder for an arbitrary address or email address when a user logs in using an email address. Something like this in your reg form clean () method will make a unique username from an email address (since you cannot have usernames with nonalphanumerics):

highest_user_id = User.objects.all().order_by('-id')[0].id #or something more efficient
leading_part_of_email = user_email.split('@',1)[0]
leading_part_of_email = re.sub(r'[^a-zA-Z0-9+]', '', leading_part_of_email) #remove non-alphanumerics
truncated_part_of_email = leading_part_of_email[:3] + leading_part_of_email[-3:] #first three and last three - will turn "ab@foo.com" into "abab" or b@ into "bb", which is ok
derived_username = '%s%s' % (truncated_part_of_email, highest_user_id+1)
+2
source

Django 1.2, @, +,. - .

.

0

Source: https://habr.com/ru/post/1742955/


All Articles