Django 1.7 将代码放在哪里以编程方式添加组?

2022-01-25 00:00:00 python django django-models django-admin

问题描述

我一直试图在 Django Auth 文档中找到答案,但似乎找不到我要找的东西.

I have been trying to find the answer in the Django Auth docs, but can not seem to find what I am looking for.

我遇到的问题是,当我定义添加组的代码时(与管理页面中的组相同):

The problem I am having is, when I define the code for adding Groups (same as Groups in the admin page):

#read_only
group, created = Group.objects.get_or_create(name='read_only')   
if created:
    group.permissions.add(can_read_campaign)
    logger.info('read_only_user Group created')
#standard
group, created = Group.objects.get_or_create(name='standard_user') 
if created:
    group.permissions.add(can_edit_users)
    logger.info('standard_user Group created')
#admin
group, created = Group.objects.get_or_create(name='admin_user') 
if created:
    group.permissions.add(can_edit_campaign, can_edit_users)
    logger.info('admin_user Group created')

当我在 models.py 和 init.py 中运行此代码时,它们都给了我这个错误:

When I have run this code in models.py and init.py and they both give me this error:

django.core.exceptions.AppRegistryNotReady

我认为这是由于 Model/init 试图过早将内容插入 django 应用程序/管理?

I presume this is due to the Model/init trying to insert things into the django app/admin too early?

如何以编程方式添加这些组?

How can I add these Groups programmatically?

这不是一个重复的问题,这实际上是在项目设置期间在模型中添加权限和组,而不是通过 shell.

This is not a duplicate question, this was actually adding permission and groups within the models during setup of the project, rather than through the shell.

我已经通过使用信号和接收器(django 模块)解决了这个问题.

I have solved this issues, by using signals and receivers (django modules).

我将用于创建权限/组的代码添加到它自己的函数中,并用接收器 (post_migrate) 对其进行修饰,它将在迁移完成后运行此函数,从而消除此错误.

I added the code to create the permissions/groups into it's own function and decorated this with a receiver (post_migrate), which will run this function after migrations are complete, removing this error.

@receiver(post_migrate)
def init_groups(sender, **kwargs):
    #permission and group code goes here


解决方案

我被推荐 这个 方法:

在适当的模块中创建一个假迁移:

Create a fake migration in the appropriate module:

python manage.py makemigrations --empty yourappname

打开创建的文件,应该是这样的:

Open up the file that was created, which should look like this:

# -*- coding: utf-8 -*-
from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('yourappname', '0001_initial'),
    ]

    operations = [
    ]

并添加您的代码:

# -*- coding: utf-8 -*-
from django.db import models, migrations

def add_group_permissions():
    #read_only
    group, created = Group.objects.get_or_create(name='read_only')   
    if created:
        group.permissions.add(can_read_campaign)
        logger.info('read_only_user Group created')

    #standard
    group, created = Group.objects.get_or_create(name='standard_user') 
    if created:
        group.permissions.add(can_edit_users)
        logger.info('standard_user Group created')

    #admin
    group, created = Group.objects.get_or_create(name='admin_user') 
    if created:
        group.permissions.add(can_edit_campaign, can_edit_users)
        logger.info('admin_user Group created')

class Migration(migrations.Migration):

    dependencies = [
        ('yourappname', '0001_initial'),
    ]

    operations = [
        migrations.RunPython(add_group_permissions),
    ]

最后,运行迁移:

python manage.py migrate

这很好,因为您可以部署到 Heroku 或任何地方,并确保它会被应用,因为这只是另一个迁移.

This is nice because you can deploy to Heroku or wherever and be sure it'll be applied, as it's just another migration.

相关文章