2016-08-23 93 views
0

伙计们,我试图删除默认的django权限,我永远不会在我的项目中使用,但没有成功。当我进行移植时,它指出移植是成功的,但没有效果,就像跳过了这个功能。我很确定代码是可以的,因为我在shell中测试它。有任何想法吗? 这里的代码迁移:从auth模型中删除django默认权限

from django.db import migrations 


def remove_redundant_permissions(apps, schema_editor): 
    Permission = apps.get_model('auth.Permission') 
    app_labels = ['admin', 'reversion', 'contenttypes', 'sessions', 'sites'] 
    Permission.objects.filter(content_type__app_label__in=app_labels).delete() 


class Migration(migrations.Migration): 

dependencies = [ 
    ('users', '0014_auto_20160808_0738'), 
] 

operations = [ 
    migrations.RunPython(remove_redundant_permissions), 
] 

回答

0

Django的permessions与post_migrate信号生成的。这意味着即使您在迁移时删除它们,它们也会在迁移完成后重新生成。

下面是来自Django的

# django/contrib/auth/apps.py 
from django.apps import AppConfig 
from django.contrib.auth.checks import check_user_model 
from django.core import checks 
from django.db.models.signals import post_migrate 
from django.utils.translation import ugettext_lazy as _ 

from .management import create_permissions 


class AuthConfig(AppConfig): 
    name = 'django.contrib.auth' 
    verbose_name = _("Authentication and Authorization") 

    def ready(self): 
     post_migrate.connect(create_permissions, 
      dispatch_uid="django.contrib.auth.management.create_permissions") 
     checks.register(check_user_model, checks.Tags.models) 

代码正如你看到auth应用程序有这个AppConfig其再生权限与create_permissions功能。

为什么要删除默认的django权限?他们阻止你做某事吗?

+0

只是整容的原因,谢谢你回答我的问题,我想我会让他们那么。 –