How to move model to the other section in Django's site admin
Asked Answered
W

2

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.

Wendy answered 24/1, 2015 at 1:21 Comment(2)
What is the URL structure that you are looking for? So 2 Apps will be under Accounts?Ignacia
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 #2553016Septuplicate
G
17

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

Gentilism answered 22/11, 2015 at 15:47 Comment(7)
Thanks. This is what I was looking for.Wendy
from django.db.models.loading import get_models is deprecated in Django 1.9 and upperGreenwood
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 = '<your-new-app-name>'Immersionism
@Immersionism Thanks so much.Toothless
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?Villanovan
@pspahn, your question is not clear and you didn't mention a person who you were asking.Gentilism
@Immersionism This unfortunately leads to new migration files in the Django auth directory itself. Not a good idea.Alys
P
0

For the case you want your model to sit in same models , but shown under different app without migration, you can do:

class YourModelProxy(YourModel):
   class Meta:
       proxy = true
       app_label = "new_app_label"
       verbose_name = "Your model"

You use this for django admin

Provisory answered 21/6 at 18:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.