Skip to content
Advertisement

Group and Permissions Assignment Missing when using Custom User Model

I am building an app with multiple roles defined through Django Groups. I started with a custom user model, defined as below.

I am seeing a weird difference in the groups and permissions use when using a custom user model, like the inheritance is missing something.

I would like to use a custom user model so I don’t use username but I also need multiple groups and permissions in my application.

from django.db import models
from django.contrib.auth.models import AbstractUser, AbstractBaseUser, BaseUserManager, PermissionsMixin
import random
import string
from slugify import slugify


# Create your models here.

class MyAccountManager(BaseUserManager):
    def create_user(self, email, password=None):
        if not email:
            raise ValueError("You must have an email address")
        user = self.model(
            email=self.normalize_email(email),
        )
        user.set_password(password)
        user.save(using=self._db)
        return user
    
    def create_superuser(self, email, password):
        user = self.create_user(
            email=self.normalize_email(email),
        )
        user.set_password(password)
        user.is_admin=True
        user.is_staff=True
        user.is_superuser=True
        user.save(using=self._db)
        return user


#custom user account model
class User_Account(AbstractUser):
    email = models.EmailField(verbose_name='email', max_length=60, unique = True)
    date_joined = models.DateTimeField(verbose_name="Date Joined", auto_now_add=True)
    last_login = models.DateTimeField(verbose_name="Last Login", auto_now=True)
    username = models.CharField(max_length=100, null=True)
    is_admin = models.BooleanField(default=False)
    is_active = models.BooleanField(default=True)
    is_staff = models.BooleanField(default=False)
    is_superuser = models.BooleanField(default=False)

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = []
    
    objects = MyAccountManager()

    def __str__(self):
        return self.email

When I create a new group with the custom user model, the group gets automatically assigned to all created users. I reproduced this behavior both programmatically and through the Django Admin. When I use the default user model the group creation doesn’t assign groups to all users automatically. I also discovered that when using a custom user model the Django Admin for the users is not the same (the group and permission assignment fields are incomplete – screenshots below) Weird incomplete Django Admin interface with groups and permissions missing the available fields

Normal Django Admin interface with group and permission assignment as expected – default user model

Advertisement

Answer

I managed to fix the issue in the Admin panel. It seems that it’s a visual rendering problem caused by a wrong Admin class.

The error was caused by the following:

    filter_horizontal = ()
    list_filter = ()
    fieldsets = ()

I have actually added the proper parameters in the class above but forgot to comment out/remove these lines. Works properly after commenting them out.

Advertisement