15

Is it possible to move default Groups model from 'Authentication and Authoriation' section (on the Django admin site) to custom one and how to achieve that?

Let's start from the beginning in the other words.

I have a very simple application 'accounts' in my Django project.

models.py file looks like below:

from django.contrib.auth.models import AbstractUser


class User(AbstractUser):
    def __str__(self):
        return self.email

serializers.py file:

from rest_framework import serializers
from django.contrib.auth.models import Group
from django.contrib.auth import get_user_model


class GroupSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Group


class UserSerializer(serializers.HyperlinkedModelSerializer):
    groups = serializers.HyperlinkedRelatedField(
        many=True,
        required=False,
        read_only=True,
        view_name="group-detail"
    )

    class Meta:
        model = get_user_model()
        exclude = ('user_permissions',)

Now, on the admin site I have two sections: 'Accounts' and 'Authentication and Authorization'. 'Accounts' section contains my 'Users' table (for User model) and 'Authentication and Authorization' section contains 'Groups' table (for Django's default authorization Group model).

My question is - is it possible and how to move Groups table (model) to the 'Accounts' section?

I've even tried to create a custom 'Group' model based on Django's default auth Group model but have stuck on migration exceptions.

Serjik
  • 10,543
  • 8
  • 61
  • 70
Tomasz Dzieniak
  • 2,765
  • 3
  • 24
  • 42
  • What is the URL structure that you are looking for? So 2 Apps will be under Accounts? – Aaron Lelevier Jan 24 '15 at 01:37
  • I think what your are asking for is to display the User model in the Admin site with more features. To do that you need to unregister the User model and then create a AdminModel with the desired fields then register the User model again. See http://stackoverflow.com/questions/2552516/changing-user-modeladmin-for-django-admin – abdul Nov 19 '15 at 22:35

1 Answers1

16

Is it possible to move default Groups model from 'Authentication and Authoriation' section (on the Django admin site) to custom one and how to achieve that?

Yes, it's possible.

1) You can move your model to section auth, just add to your class:

class Meta:
    app_label = 'auth'

2) You can move Group and User models to your app section, for that variant need to:

Override user model and add it to your app

from django.contrib.auth.models import AbstractUser


class CustomUser(AbstractUser):
    pass

also need add to your project settings AUTH_USER_MODEL = 'your_app.CustomUser'

Don't forget declare in admin.py from your app:

class UserAdmin(admin.ModelAdmin):
    pass


admin.site.register(CustomUser, UserAdmin)

For group model put this code in admin.py:

from django.db.models.loading import get_models
from django.contrib.auth import models

models = get_models(models)
models[1]._meta.app_label = 'your_app'

3) You can look at django-admin-tools

ptim
  • 14,902
  • 10
  • 83
  • 103
Greg Eremeev
  • 1,760
  • 5
  • 23
  • 33
  • Thanks. This is what I was looking for. – Tomasz Dzieniak Nov 22 '15 at 19:43
  • `from django.db.models.loading import get_models` is deprecated in Django 1.9 and upper – Tarek Kalaji Jan 31 '18 at 12:44
  • 8
    To move the group model in django 2+ this works fine to put in your admin.py. `from django.apps import apps` `apps.get_model('auth.Group')._meta.app_label = ''` – D.Nibon Feb 10 '18 at 15:12
  • @D.Nibon Thanks so much. – Adam Aug 15 '18 at 23:35
  • I take it that you can't move a model to an arbitrary section that isn't installed in `INSTALLED_APPS`, is that right? So if I have all my models in a single app and just want to organize them in the admin without splitting models off to their own app, I can't just add it to any undefined section? – pspahn Apr 30 '20 at 20:23
  • @pspahn, your question is not clear and you didn't mention a person who you were asking. – Greg Eremeev May 01 '20 at 09:18
  • 6
    @D.Nibon This unfortunately leads to new migration files in the Django auth directory itself. Not a good idea. – medihack Jun 16 '20 at 09:08