3

I'm using Django 2.x

I have created a custom user model by extending the AbstractModel in authentication application

class User(AbstractUser):
    pass

and updated in settings

AUTH_USER_MODEL = 'authentication.User'

This has resulted in two sections in the admin panel. One section authentication contains User model while default Authentication and Authorization contain only Group model.

I want to move either User to Authentication and Authorization or Group to Authentication so that both models could be together in a section.

For that, I have added this in the authentication.admin.py

apps.get_model('auth.Group')._meta.app_label = 'authentication'

To move Group to Authentication.

After running

python manage.py makemigrations

It generates migration in the auth app of django

Migrations for 'auth':
  /Users/anuj/.local/share/virtualenvs/originor_py-Vd6fDdN7/lib/python3.6/site-packages/django/contrib/auth/migrations/0010_auto_20190220_0238.py
    - Remove field permissions from group
    - Alter field groups on user
    - Delete model Group

And on migrating the migrations ./manage.py migrate, it gives error as

ValueError: The field auth.User.groups was declared with a lazy reference to 'authentication.group', but app 'authentication' doesn't provide model 'group'.

How can I move Group model to another section in the Django admin?
Do I need to create custom Group model?

Anuj TBE
  • 9,198
  • 27
  • 136
  • 285

1 Answers1

0

You have overridden AbstractUser which inherits AbstractBaseUser and PermissionsMixin as below class AbstractUser(AbstractBaseUser, PermissionsMixin):

You could see groups attribute in django.contrib.auth.models.PermissionsMixin

class PermissionsMixin(models.Model):
    """
    A mixin class that adds the fields and methods necessary to support
    Django's Group and Permission model using the ModelBackend.
    """
    ...
    groups = models.ManyToManyField(
        Group,
        verbose_name=_('groups'),
        blank=True,
        help_text=_(
            'The groups this user belongs to. A user will get all permissions '
            'granted to each of their groups.'
        ),
        related_name="user_set",
        related_query_name="user",
    )
    ...

Create CustomGroup model and add that model to groups attribute as follow.

class User(AbstractUser):
    ...
    groups = models.ManyToManyField(
        CustomGroup,
        verbose_name=_('groups'),
        blank=True,
        help_text=_(
            'The groups this user belongs to. A user will get all permissions '
            'granted to each of their groups.'
        ),
        related_name="user_set",
        related_query_name="user",
    )
    ...

This would help you to override default Group model altered with your CustomGroup model

Devang Padhiyar
  • 3,427
  • 2
  • 22
  • 42
  • But I do not want to override default group and just want to put it in a different section in the Django Admin panel. – Anuj TBE Feb 20 '19 at 09:03