• <small id='2eJJh'></small><noframes id='2eJJh'>

      • <bdo id='2eJJh'></bdo><ul id='2eJJh'></ul>

        <i id='2eJJh'><tr id='2eJJh'><dt id='2eJJh'><q id='2eJJh'><span id='2eJJh'><b id='2eJJh'><form id='2eJJh'><ins id='2eJJh'></ins><ul id='2eJJh'></ul><sub id='2eJJh'></sub></form><legend id='2eJJh'></legend><bdo id='2eJJh'><pre id='2eJJh'><center id='2eJJh'></center></pre></bdo></b><th id='2eJJh'></th></span></q></dt></tr></i><div id='2eJJh'><tfoot id='2eJJh'></tfoot><dl id='2eJJh'><fieldset id='2eJJh'></fieldset></dl></div>
        <legend id='2eJJh'><style id='2eJJh'><dir id='2eJJh'><q id='2eJJh'></q></dir></style></legend>
      1. <tfoot id='2eJJh'></tfoot>

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

        Django 1.7 where to put the code to add Groups programmatically?(Django 1.7 将代码放在哪里以编程方式添加组?)

        <small id='aIO19'></small><noframes id='aIO19'>

      2. <legend id='aIO19'><style id='aIO19'><dir id='aIO19'><q id='aIO19'></q></dir></style></legend>

          <bdo id='aIO19'></bdo><ul id='aIO19'></ul>
          <tfoot id='aIO19'></tfoot>
            <tbody id='aIO19'></tbody>
          <i id='aIO19'><tr id='aIO19'><dt id='aIO19'><q id='aIO19'><span id='aIO19'><b id='aIO19'><form id='aIO19'><ins id='aIO19'></ins><ul id='aIO19'></ul><sub id='aIO19'></sub></form><legend id='aIO19'></legend><bdo id='aIO19'><pre id='aIO19'><center id='aIO19'></center></pre></bdo></b><th id='aIO19'></th></span></q></dt></tr></i><div id='aIO19'><tfoot id='aIO19'></tfoot><dl id='aIO19'><fieldset id='aIO19'></fieldset></dl></div>

                • 本文介绍了Django 1.7 将代码放在哪里以编程方式添加组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我一直试图在 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.

                  这篇关于Django 1.7 将代码放在哪里以编程方式添加组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  What happens when you compare 2 pandas Series(当你比较 2 个 pandas 系列时会发生什么)
                  Quickly find differences between two large text files(快速查找两个大文本文件之间的差异)
                  Python - Compare 2 files and output differences(Python - 比较 2 个文件和输出差异)
                  Why do comparisions between very large float values fail in python?(为什么在 python 中非常大的浮点值之间的比较会失败?)
                  Dictionary merge by updating but not overwriting if value exists(字典通过更新合并,但如果值存在则不覆盖)
                  Find entries of one text file in another file in python(在python中的另一个文件中查找一个文本文件的条目)

                      <small id='S0CnQ'></small><noframes id='S0CnQ'>

                            <tbody id='S0CnQ'></tbody>
                          <i id='S0CnQ'><tr id='S0CnQ'><dt id='S0CnQ'><q id='S0CnQ'><span id='S0CnQ'><b id='S0CnQ'><form id='S0CnQ'><ins id='S0CnQ'></ins><ul id='S0CnQ'></ul><sub id='S0CnQ'></sub></form><legend id='S0CnQ'></legend><bdo id='S0CnQ'><pre id='S0CnQ'><center id='S0CnQ'></center></pre></bdo></b><th id='S0CnQ'></th></span></q></dt></tr></i><div id='S0CnQ'><tfoot id='S0CnQ'></tfoot><dl id='S0CnQ'><fieldset id='S0CnQ'></fieldset></dl></div>
                            <bdo id='S0CnQ'></bdo><ul id='S0CnQ'></ul>
                            <tfoot id='S0CnQ'></tfoot>
                          • <legend id='S0CnQ'><style id='S0CnQ'><dir id='S0CnQ'><q id='S0CnQ'></q></dir></style></legend>