在Django Admin中更改模型组 [英] Changing the group of a model in Django Admin

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

问题描述

无论出于何种原因,我都有与身份验证相关的三个模型,但是在Django Admin中,它们显示在两个不同的组中。例如:

For whatever reason I have three models related to authentication, but in Django Admin they show up in two different groups. For example:

AUTHORIZATION
-------------
Security Questions
Users

AUTHORIZATION AND AUTHENTICATION
--------------------------------
Groups

似乎他们应该属于一个组,我想

Seems like they should be under one group and I would like to move them to be under one group.

几年前,我遇到了这个问题:

I came across this Q/A from a few years ago:

在Django Admin中向用户添加模型/ Group模型?

想知道在Django 1.11中现在是否有更简单的方法,例如使用 class Meta:在模型中还是在 admin.py 中。

Wonder if there is an easier way now in Django 1.11 like using a class Meta: in the model or in admin.py. Looking through the documentation and haven't come across anything yet.

推荐答案

我实际上不得不结合Mohammad和NeErAj的建议。

I actually had to combine what both Mohammad and NeErAj suggested.

当我尝试将移至授权部分,这是一个包含自定义 User 模型的应用程序,它创建了组的重复项。 Django仍然想插入默认的 auth_group ,我不知道该如何摆脱。

When I tried to move Groups to the Authorization section, which is an app containing a custom User model, it created duplicates of the Groups. Django still wanted to insert the default auth_group which I couldn't figure out how to get rid of.

# ./models.py
from django.contrib.auth.models import Group

class Group(Group):
    pass

    class Meta:
    app_label = 'authentication'







# ./admin.py
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from .models import User, SecurityQuestions, Group

admin.site.register(User, UserAdmin)
admin.site.register(SecurityQuestions)
admin.site.register(Group)







AUTHORIZATION
-------------
Groups
Security Questions
Users

AUTHORIZATION AND AUTHENTICATION
--------------------------------
Groups

由于我使用的是自定义的 User 模型,所以我认为这很容易将它们移动到 app_label ='auth'。这样,我就不必使用默认的 auth_group 。最终执行以下操作:

Since I was using a custom User model, I figured it would be easier to move them to app_label = 'auth'. That way I wouldn't have to fight with the default auth_group. Ended up doing the following:

# ./models.py
from django.contrib.auth.models import AbstractUser

class User(AbstractUser):
    ...

    class Meta:
        db_table = 'Users'

class SecurityQuestions(models.Model):
    ...

    class Meta:
        app_label = 'auth'
        db_table = 'Security_Questions'
        verbose_name = 'Security Question'
        verbose_name_plural = 'Security Questions'

 class ProxyUser(User):
     pass

     class Meta:
         app_label = 'auth'
         proxy = True
         verbose_name = 'User'
         verbose_name_plural = 'Users'







# ./admin.py

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from .models import User, SecurityQuestions, ProxyUser

admin.site.register(ProxyUser, UserAdmin)
admin.site.register(SecurityQuestions)

这一切看起来像:

AUTHORIZATION AND AUTHENTICATION
--------------------------------
Groups
Security Questions
Users

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

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