0

I create a custom user model to login via email, but i got some issue when im tried to login on admin channel

from django.db import models
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager
class MyAccountManajer(BaseUserManager):
    def create_user(self, first_name, last_name, username, email, password=None):
        if not email:
            raise ValueError('User must have an email address')
        
        if not username:
            raise ValueError('User must have an username')

        user = self.model(
            email = self.normalize_email(email),
            username = username,
            first_name = first_name,
            last_name = last_name,
        )
        user.set_password(password)
        user.save(using=self.db)
        return user

    def create_superuser(self, first_name, last_name, username, email, password):
         user = self.create_user(
            email = self.normalize_email(email),
            password= password,
            username = username,
            first_name = first_name,
            last_name = last_name,
         )
         user.is_admin = True
         user.is_active = True
         user.is_staff = True
         user.is_superadmin = True
         user.save(using=self.db)
         return user

I'm able to create a superuser. However, when I try to login with email. I got this Error Exception Type: AttributeError Exception Value: 'Account' object has no attribute 'has_module_perms' Exception Location: ....\env\lib\site-packages\django\utils\functional.py, line 241, in inner

Can someone fix that ?

1 Answers1

1

You need to add the "PermissionsMixin" to your class to obtain the method you need

from django.contrib.auth.models import PermissionsMixin 

class MyAccountManajer(AbstractBaseUser, PermissionsMixin):
        

You can find the answer to a similar question asked here: custom django-user object has no attribute 'has_module_perms'

DavidW
  • 29,336
  • 6
  • 55
  • 86
Etch
  • 462
  • 3
  • 12
  • it works, in my first try i ca'tn edited in django admin but the problem is i forget delete some file thank ToT – Marcus Aurelius Sep 30 '21 at 03:01
  • Happy it's working! If this is what solved your problem, check it as the answer. If not, write an answer and check it to help people facing the same issue. – Etch Sep 30 '21 at 12:53