在Django Admin中将模型添加到用户/组模型? [英] Adding a model in Django Admin to the User/Group models?

查看:435
本文介绍了在Django Admin中将模型添加到用户/组模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Django管理站点具有身份验证和授权,其中包含用户和组。当我们添加模型时,它们将显示在此下方。是否可以在用户组下直接添加模型?以及如何更改现有名称?

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?

推荐答案

这有点棘手,需要一些工作,并且会在Django 1.8中触发一些警告。但这基本上是您要做的事情:

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:

假设您有一个应用程序 myauth ,它将覆盖默认的 django.contrib.auth 应用程序...

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

myauth / models.py 执行此操作:

from django.db import models

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


class ExtraModel(models.Model):
    ...

ExtraModel 是要添加的模型。

myauth / __ init __。py 执行以下操作:

from django.apps import AppConfig

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

default_app_config = 'auth.OverrideAuthConfig'

myauth / admin.py

from django.contrib import admin

from .models import User, Group, ExtraModel

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

并在您的 settings.py 中,在 INSTALLED_APPS 在行中注释:

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

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

,只需添加您的应用程序即可:

and just add your application:

...
'myauth',
...

这篇关于在Django Admin中将模型添加到用户/组模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆