接收django post_migrate信号 [英] receiving django post_migrate signal
问题描述
我正在为我的项目进行某种形式的重构,其中依赖于django django.contrib.auth.models.Permission
模型。到目前为止,我使用post_save信号定义了每个新用户的权限,因此,在创建用户时,我使用 user.user_permissions.add(the_permission)
分配了他们的权限,
I'm doing some kind of refactoring for my project, where I'm relying on the django django.contrib.auth.models.Permission
model. So far I define the permissions for each new user using a post_save signal, so when the user is created, I assign their permissions using user.user_permissions.add(the_permission)
, this works perfectly.
现在我想使用 django.contrib.auth.models.Group
模型来分类
Now I want to use the django.contrib.auth.models.Group
model to clasify the permissions a user should have.
这是我的代码:
from django.apps import AppConfig
from django.db.models.signals import post_migrate
from django.contrib.auth.models import Group, Permission
def create_group(name, permissions):
group = Group.objects.create(name=name)
[group.permissions.add(permission) for permission in permissions]
def define_company_groups(sender, **kwargs):
permissions = [
Permission.objects.get(codename='add_mymodel'),
Permission.objects.get(codename='change_mymodel'),
]
create_group('managers', permissions)
class MyAppConfig(AppConfig):
name = 'players'
verbose_name = 'The players app'
def ready(self):
post_migrate.connect(define_company_groups, sender=self)
之后定义此代码,我希望在调用 ./ manage.py migration
之后,应触发此处理程序。但这没有发生,我所得到的只是:
After define this code, I'm expecting that after call ./manage.py migrate
this handler should be fired. But it doesn't happen, all I got is:
Running post-migrate handlers for application players
Adding permission 'players | mymodel | Can add mymodel'
Adding permission 'companies | company | Can change mymodel'
Adding permission 'companies | company | Can delete company'
我找到了这个 https://groups.google.com/forum/#!topic/django-developers/8MdaWtJp4VQ 文章,他们说我应该在名为 management.py
的文件中定义我的post_migrate处理程序,但这对我不起作用。
I found this https://groups.google.com/forum/#!topic/django-developers/8MdaWtJp4VQ article, they say I should define my post_migrate handler inside a file named management.py
, but it does not work for me.
最后,这是我的问题:我应该在哪里将此代码放入我的自定义post_migrate信号?
Finally, here's my question: Where should I put this code for my custom post_migrate signal?
推荐答案
The Django文档建议连接 post_migrate
信号指示您的应用程序配置的ready方法。您链接到的Google网上论坛发布的文档从更新文档之前就已过期。
The Django docs recommend connecting the post_migrate
signal in your app config's ready method. The Google groups post you link to is out of date, from before the docs were updated.
您还需要在您的 INSTALLED_APPS
设置中指定应用配置 。
You also need to specify the app config in your INSTALLED_APPS
setting.
INSTALLED_APPS = [
'myapp.apps.MyAppConfig',
# ...
]
另一种配置应用程序的方法是使用 default_app_config 应用中
__ init __。py
中的code>。请参阅配置应用程序。但是首选另一种方法(指向AppConfig的虚线路径)。
Another way to configure your app is to use default_app_config
in __init__.py
of your app. See Configuring Applications. But the other way (dotted path to AppConfig) is preferred.
这篇关于接收django post_migrate信号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!