Django 1.7将代码以编程方式添加到哪里? [英] Django 1.7 where to put the code to add Groups programmatically?
问题描述
我一直试图在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?
编辑:
这不是重复的问题,
我已经通过使用信号和接收器解决了这个问题(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.
这篇关于Django 1.7将代码以编程方式添加到哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!