Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

custom django-user object has no attribute 'has_module_perms'

My custom user model for login via email:

class MyUser(AbstractBaseUser):     id = models.AutoField(primary_key=True)  # AutoField?     is_superuser = models.IntegerField(default=False)     username = models.CharField(unique=True,max_length=30)     first_name = models.CharField(max_length=30, default='')     last_name = models.CharField(max_length=30, default='')     email = models.EmailField(unique=True,max_length=75)     is_staff = models.IntegerField(default=False)     is_active = models.IntegerField(default=False)     date_joined = models.DateTimeField(default=None)      # Use default usermanager     objects = UserManager()      USERNAME_FIELD = 'email'     REQUIRED_FIELDS = ['username']      class Meta:         db_table = 'auth_user' 

For which, I'm able to successfully create a superuser. However, when I try to login with email and password, I get this error:

'MyUser' object has no attribute 'has_module_perms' 

Any idea what I'm doing wrong ?

like image 940
Saurabh Verma Avatar asked Jul 12 '15 17:07

Saurabh Verma


2 Answers

Your User implementation is not providing the mandatory methods to be used with the Admin module.

See https://docs.djangoproject.com/en/3.0/ref/contrib/admin/#module-django.contrib.admin.

In your case, add the permissions mixin (PermissionsMixin), as a superclass of your model:

from django.contrib.auth.models import PermissionsMixin   class MyUser(AbstractBaseUser, PermissionsMixin):      # ... 

It is described here : https://docs.djangoproject.com/en/3.0/topics/auth/customizing/#custom-users-and-permissions.

It works with Django 1.x, 2.x and 3.0.

EDIT: updated links to django version 3.0

like image 99
Gabriel Pichot Avatar answered Nov 13 '22 01:11

Gabriel Pichot


I think your model is missing some attributes such as 'has_module_params'...Try to add the following

class MyUser(AbstractBaseUser):   ...   ...    def has_perm(self, perm, obj=None):     return self.is_superuser    def has_module_perms(self, app_label):     return self.is_superuser 
like image 42
Bogdan Goie Avatar answered Nov 13 '22 02:11

Bogdan Goie