简体   繁体   中英

Adding a model in Django Admin to the User/Group models?

The Django admin site has Authentication and Authorization which has users and groups. When we add models, they are displayed below this. Is it possible to add a model immediately below Groups, Users? And how can we change the existing names?

在此处输入图片说明

It's a bit tricky, takes some work, and it's going to trigger some warning in Django 1.8. But this is basically what you do:

Suppose you have an application myauth that will override the default django.contrib.auth application...

In myauth/models.py do this:

from django.db import models

from django.contrib.auth.models import User, Group


class ExtraModel(models.Model):
    ...

ExtraModel is the model you want to add.

In myauth/__init__.py do this:

from django.apps import AppConfig

class OverrideAuthConfig(AppConfig):
    name = 'auth'
    verbose_name = "New User and Authentication"

default_app_config = 'auth.OverrideAuthConfig'

In myauth/admin.py do this:

from django.contrib import admin

from .models import User, Group, ExtraModel

admin.site.register(User)
admin.site.register(Group)
admin.site.register(ExtraModel)

And in your settings.py , within INSTALLED_APPS comment out the line:

...
#'django.contrib.auth',
...

and just add your application:

...
'myauth',
...

这是结果

Try to override admin index template ...

change this {% for app in app_list %} to {% for app in app_list reversed %} or sort it in some custom way ...


EDIT

seems I've found easier solution :)

https://github.com/mishbahr/django-modeladmin-reorder

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM