Email and Username as Authenticator

Hello Guys, i found some thread online, and i tried more than once and failed, i think its easy overall but i am still locked :S
Trying to authenticate users with either email or username.
When i say Users i got a custom user, base user + profile with just a few more info about the user submitted through registration form:

class ProfiloUtente(models.Model):
user = models.OneToOneField(User, related_name=‘profile’, on_delete=models.CASCADE)
utente = models.ForeignKey(Utente, on_delete=models.CASCADE)
studio = models.ManyToManyField(Studio)
telefono_fisso = models.CharField(max_length=20, default=None, blank=True)
telefono_mobile = models.CharField(max_length=20, default=None, blank=True)
indirizzo = models.CharField(max_length=40, default=None, blank=True)
citta = models.CharField(max_length=50, default=None, blank=True)
cap = models.CharField(max_length=5, default=None, blank=True)
provincia = models.CharField(max_length=30, default=None, blank=True)
cod_fiscale = models.CharField(max_length=16, default=None, blank=True)
p_iva = models.CharField(max_length=27, default=None, blank=True)
def str(self):
return self.user.username

And this is my data about the default user fields:

class RegistrationForm(UserCreationForm):
email = forms.EmailField(required=True)
first_name = forms.CharField(max_length=30, required=True)
last_name = forms.CharField(max_length=100, required=True)

class Meta:
    model = User
    fields = (
        'username',
        'email',
        'first_name',
        'last_name',
        'password1',
        'password2',

    )


The login form is the django default login with username + psw.
Could someone help me understanding what i could do to extend the field username to also take the email into it?
Thanks

Are you talking about allowing users to login to the site using either their username or their email address?

If so, my first attempt on this would be to create a custom AuthenticationForm that inherits from the built-in AuthenticationForm, overriding the clean method to try authenticating the person both ways and allowing them to be logged in if either attempt worked.

Ken

2 Likes

@KenWhitesell, ill try it soon and notice you, it should be a fast work.
Thanks a lot!

may you help me to get a better understanding of what i should do? on a large scale i could understand but when it comes to practice i don’t know how to move myself through it.
Ill repost with some code snippets

When you do post code snippets, please insert them between lines containing only 3 backticks - ```. (Make sure you use the backtick - ` and not the apostrophe - '.) That means you would have one line with the 3 backticks, your code, and then one more line with just the 3 backticks. That allows this forum software to format your code nicely:

# The line above this is just ```
def function(parm):
    return parm
# The line after this is just ```

So yes, go ahead and post what you’ve got and we can work forward from there.

Ken

Sry Ken, i should have posted it here, but its a long thread and perhaps could help some other people if the visibility is about a post on it’s own.
I’ll link it here

Is there a benefit to using the AuthenticationForm approach rather than a custom backend?

A lot less work. The custom authentication form ends up being one method in one form consisting of maybe 15 lines of code. (See the linked thread for the continuation of this discussion.)

1 Like