1
votes

I am creating the registration,login, logout which requires only email and password. When I use user = models.OneToOneField(User,on_delete=models.CASCADE) it gives me an error because it requires username too. How can I take only email and password from User and save it to user?

1
simple way is to normalize the email and assign it to username as a default value, with respect to unique constraint. The better solution is to extend AbstractUser to a model of yours, let's name it MyUser, then add USERNAME_FIELD = 'email' to MyUser and change your current user = models.OneToOneField(User,on_delete=models.CASCADE) to user = models.OneToOneField(MyUser,on_delete=models.CASCADE). You also need to change AUTH_USER_MODEL in your settingsSajad

1 Answers

1
votes

You can use

AUTHENTICATION_BACKENDS = ('path_to_file.EmailAuthBackend', 'django.contrib.auth.backends.ModelBackend',)

in settings

and backend file:

from django.contrib.auth.backends import ModelBackend
from django.contrib.auth import get_user_model
from django.contrib.auth.hashers import check_password
from django.contrib.auth.models import User

class EmailAuthBackend(object):

    @staticmethod
    def authenticate(self, username="", password="", **kwargs):
        user_model = get_user_model()

        try:
            user = user_model.objects.get(email=username)
            if check_password(password, user.password):
                return user
            else:
                return None
        except user_model.DoesNotExist:
            return None

    @staticmethod
    def get_user(user_id):
        try:
            return User.objects.get(pk=user_id)
        except User.DoesNotExist:
            return None

and add unique email rule in your models

from django.contrib.auth.models import User    
User._meta.get_field('email')._unique = True