简体   繁体   English

django管理员将身份验证模型移至另一部分

[英]django admin move auth models to another section

I'm using Django 2.x 我正在使用Django 2.x

I have created a custom user model by extending the AbstractModel in authentication application 我通过扩展authentication应用程序中的AbstractModel来创建了自定义用户模型

class User(AbstractUser):
    pass

and updated in settings 并在设置中更新

AUTH_USER_MODEL = 'authentication.User'

This has resulted in two sections in the admin panel. 这导致了管理面板中的两个部分。 One section authentication contains User model while default Authentication and Authorization contain only Group model. 一节身份验证包含用户模型,而默认身份验证和授权仅包含模型。

I want to move either User to Authentication and Authorization or Group to Authentication so that both models could be together in a section. 我想将“ 用户”移到“ 身份验证和授权”或“ 组”到“ 身份验证”,以便两个模型可以在一个部分中在一起。

For that, I have added this in the authentication.admin.py 为此,我将其添加到了authentication.admin.py

apps.get_model('auth.Group')._meta.app_label = 'authentication'

To move Group to Authentication . 移至身份验证

After running 跑步后

python manage.py makemigrations

It generates migration in the auth app of django 它在Django的auth应用中生成迁移

Migrations for 'auth':
  /Users/anuj/.local/share/virtualenvs/originor_py-Vd6fDdN7/lib/python3.6/site-packages/django/contrib/auth/migrations/0010_auto_20190220_0238.py
    - Remove field permissions from group
    - Alter field groups on user
    - Delete model Group

And on migrating the migrations ./manage.py migrate , it gives error as 并且在迁移迁移./manage.py migrate ,它给出错误,因为

ValueError: The field auth.User.groups was declared with a lazy reference to 'authentication.group', but app 'authentication' doesn't provide model 'group'.

How can I move Group model to another section in the Django admin? 如何将Group模型移到Django管理员的另一部分?
Do I need to create custom Group model? 我需要创建自定义组模型吗?

You have overridden AbstractUser which inherits AbstractBaseUser and PermissionsMixin as below class AbstractUser(AbstractBaseUser, PermissionsMixin): 您已重写AbstractUserclass AbstractUser(AbstractBaseUser, PermissionsMixin):继承AbstractBaseUserPermissionsMixin ,如下class AbstractUser(AbstractBaseUser, PermissionsMixin):

You could see groups attribute in django.contrib.auth.models.PermissionsMixin 您可以在django.contrib.auth.models.PermissionsMixin看到groups属性

class PermissionsMixin(models.Model):
    """
    A mixin class that adds the fields and methods necessary to support
    Django's Group and Permission model using the ModelBackend.
    """
    ...
    groups = models.ManyToManyField(
        Group,
        verbose_name=_('groups'),
        blank=True,
        help_text=_(
            'The groups this user belongs to. A user will get all permissions '
            'granted to each of their groups.'
        ),
        related_name="user_set",
        related_query_name="user",
    )
    ...

Create CustomGroup model and add that model to groups attribute as follow. 创建CustomGroup模型,并将该模型添加到groups属性,如下所示。

class User(AbstractUser):
    ...
    groups = models.ManyToManyField(
        CustomGroup,
        verbose_name=_('groups'),
        blank=True,
        help_text=_(
            'The groups this user belongs to. A user will get all permissions '
            'granted to each of their groups.'
        ),
        related_name="user_set",
        related_query_name="user",
    )
    ...

This would help you to override default Group model altered with your CustomGroup model 这将帮助您覆盖用CustomGroup模型更改的默认Group模型

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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