定义一个自定义post_migrate信号 [英] defining a custom post_migrate signal

查看:897
本文介绍了定义一个自定义post_migrate信号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的项目做一些重构,在那里我依靠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 migrate 之后,应该触发这个处理程序。但是没有发生,我所得到的只是:

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 文章,他们说我应该在一个名为 manage.py (不包括 models.py 文件中的代码)的文件中定义我的post_migrate处理程序,但是不起作用。

I find 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 (neither including the code in the models.py file), but not works.

最后,这是我的问题:我应该把这段代码放在我自定义的post_migrate信号中?

Finally, here's my question: Where should I put this code for my custom post_migrate signal?

提前感谢

推荐答案

Django docs 建议您在应用程序中连接 post_migrate 信号config的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 设置中指定app config

You also need to specify the app config in your INSTALLED_APPS setting.

INSTALLED_APPS = [
    'myapp.apps.MyAppConfig',
    # ...
]

配置您的应用程序的另一种方法是使用 default_app_config 在您的应用程序的 __ init __。py 中。请参阅配置应用程序。但是另一种方式(虚线路径到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.

这篇关于定义一个自定义post_migrate信号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆