Search code examples
pythondjangodjango-rest-frameworkdjango-users

Django User Model Inheritance not working


In my Django app, i have two models Retailer and Customer. The model fields for both are different but for authentication the common fields are email and password. So for that I used User Model Inheritance. This is my code looks like

Reason for Inheritance : I want to implement Token Authentication

models.py

class User(AbstractUser):
    
    email = models.EmailField(_('email address'), unique=True)

    USERNAME_FIELD = 'email'
    EMAIL_FIELD = 'email'
    REQUIRED_FIELDS = []

    objects = UserManager()

    def __str__(self):
        return self.email

class Retailer(User, PermissionsMixin):

    name = models.CharField(max_length=255)
    phoneNUMBER = models.CharField(max_length=10)
    verificationSTATUS = models.BooleanField(default=False)
    dateJOINED = models.DateField(default=timezone.now)

    USERNAME_FIELD = 'email'
    EMAIL_FIELD = 'email'    
 
    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['name', 'phoneNUMBER',]
 
    objects = RetailerManager()
 
    def __str__(self):
        return self.name
 
class Customer(User, PermissionsMixin):

    name=models.CharField( max_length=255, null=False,)
    phone_Number=models.CharField( max_length=10, null=False)
    address=models.TextField(max_length=255,null=False)
    pin_Code=models.CharField( max_length=6,null=False )
    anniversary_Date=models.DateField(blank=True,null=True)
    hobbies=models.TextField(blank=True,null=True)
    profession=models.CharField(blank=True,max_length=20,null=True,)
    created_At=models.DateField(default=timezone.now)

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['name', 'phoneNUMBER', 'address', 'pin_Code']    

    objects = CustomerManager()

    def __str__(self):
        return (self.name)   

managers.py

class UserManager(BaseUserManager):
    """
    Custom user model manager where email is the unique identifiers
    for authentication instead of usernames.
    """
    def create_user(self, email, password, **extra_fields):
        """
        Create and save a User with the given email and password.
        """
        if not email:
            raise ValueError(_('The Email must be set'))
        email = self.normalize_email(email)
        user = self.model(email=email, **extra_fields)
        user.set_password(password)
        user.save()
        return user

    def create_superuser(self, email, password, **extra_fields):
        """
        Create and save a SuperUser with the given email and password.
        """
        extra_fields.setdefault('is_staff', True)
        extra_fields.setdefault('is_superuser', True)
        extra_fields.setdefault('is_active', True)

        if extra_fields.get('is_staff') is not True:
            raise ValueError(_('Superuser must have is_staff=True.'))
        if extra_fields.get('is_superuser') is not True:
            raise ValueError(_('Superuser must have is_superuser=True.'))
        return self.create_user(email, password, **extra_fields)
 

class RetailerManager(BaseUserManager):
 
    def create_retailer(self, name, email, phoneNUMBER, password=None):
        if email is None:
            raise TypeError('User must have an email address.')
        retailer = Retailer(
            email=self.normalize_email(email),
            name=name,
            phoneNUMBER=phoneNUMBER
            )
        retailer.set_password(password)
        retailer.save()
        return retailer

class CustomerManager(BaseUserManager):
 
    def create_customer(self, name, email, phone_Number, address, pin_Code, password=None, **extra_fields):
        if email is None:
            raise TypeError('User must have an email address.')
        customer = Customer(
            email=self.normalize_email(email),
            name=name,
            phone_Number=phone_Number,
            address=address,
            pin_Code=pin_Code,
            )
        customer.set_password(password)
        customer.save()
        return customer

When i run this

 Retailer.objects.create(email="johndoe@gmail.com", name="John Doe", password="john@123", phoneNUMBER = "90909090")

I get this error

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

Also, When i run this

Customer.objects.create(email="janedoe@gmail.com", name="Jane Doe", password="jane@123")

I get the same error

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

Can anyone tell what I'm doing wrong? Is this the best approach? Also how do i implement token authentication for both Retailer and Customer?


Solution

  • You need to inherit User from AbstractBaseUser not AbstractUser to use authentication with email instead of username:

    The username field raising your error comes from AbstractUser class.

    class User(AbstractBaseUser):
        
        email = models.EmailField(_('email address'), unique=True)
        first_name = models.CharField(_('first name'), max_length=150, blank=True)
        last_name = models.CharField(_('last name'), max_length=150, blank=True)
        email = models.EmailField(_('email address'), blank=True)
        is_staff = models.BooleanField(
            'staff status',
            default=False,
            help_text='Designates whether the user can log into this admin site.',
        )
        is_active = models.BooleanField(
            'active',
            default=True,
            help_text='Designates whether this user should be treated as active. Unselect this instead of deleting accounts.',
        )
        date_joined = models.DateTimeField('date joined', default=timezone.now)
        USERNAME_FIELD = 'email'
        EMAIL_FIELD = 'email'
        REQUIRED_FIELDS = ['email']
    
        objects = UserManager()
    
        def get_full_name(self):
            """
            Return the first_name plus the last_name, with a space in between.
            """
            full_name = '%s %s' % (self.first_name, self.last_name)
            return full_name.strip()
    
        def get_short_name(self):
            """Return the short name for the user."""
            return self.first_name
    
        def __str__(self):
            return self.email