定义自定义post_migrate信号

时间:2022-11-17 08:12:22

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 django.contrib.auth.models.Permission模型。到目前为止,我使用post_save信号为每个新用户定义权限,因此在创建用户时,我使用user.user_permissions.add(the_permission)分配他们的权限,这非常有效。

Now I want to use the django.contrib.auth.models.Group model to clasify the permissions a user should have.

现在我想使用django.contrib.auth.models.Group模型来区分用户应具有的权限。

This is my code:

这是我的代码:

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)

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:

在定义了这段代码之后,我希望在调用./manage.py之后,应该触发这个处理程序。但它没有发生,我得到的只是:

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'

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.

我发现这篇https://groups.google.com/forum/#!topic/django-developers/8MdaWtJp4VQ文章,他们说我应该在名为management.py的文件中定义我的post_migrate处理程序(两者都不包括模型中的代码)。 py文件),但不起作用。

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

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

Thanks in advance.

提前致谢。

1 个解决方案

#1


8  

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.

Django文档建议在app config的ready方法中连接post_migrate信号。在您更新文档之前,您链接到的Google论坛已过期。

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

您还需要在INSTALLED_APPS设置中指定应用程序配置。

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

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.

配置应用程序的另一种方法是在应用程序的__init__.py中使用default_app_config。请参阅配置应用。但另一种方式(AppConfig的虚线路径)是首选。

#1


8  

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.

Django文档建议在app config的ready方法中连接post_migrate信号。在您更新文档之前,您链接到的Google论坛已过期。

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

您还需要在INSTALLED_APPS设置中指定应用程序配置。

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

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.

配置应用程序的另一种方法是在应用程序的__init__.py中使用default_app_config。请参阅配置应用。但另一种方式(AppConfig的虚线路径)是首选。