django - 运行迁移时“StateApps”对象没有属性 'label'

标签 django django-migrations django-permissions

当我尝试运行此迁移时:

def add_user_data(apps, schema_editor):
    Group = apps.get_model('auth', 'Group')
    Permission = apps.get_model(
        'auth', 'Permission')
    Profile = apps.get_model('user', 'Profile')
    User = apps.get_model('user', 'User')
    andrew_user = User.objects.create(
        email='<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5f3b353e3138301f353e323d30312c28713c3032" rel="noreferrer noopener nofollow">[email protected]</a>',
        password=make_password('hunter2'),
        is_active=True,
        is_staff=True,
        is_superuser=True)
    andrew_profile = Profile.objects.create(
        user=andrew_user,
        name='Andrew',
        slug='andrew',
        about='The author of this site!')
    ada_user = User.objects.create(
        email='<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="62030603220608030c050d050b100e114c0d1005" rel="noreferrer noopener nofollow">[email protected]</a>',
        password=make_password('algorhythm'),
        is_active=True,
        is_staff=True,
        is_superuser=False)
    ada_profile = Profile.objects.create(
        user=ada_user,
        name='Ada Lovelace',
        slug='the_countess',
        about=(
            ' '))
    contributors = Group.objects.create(
        name='contributors')
    ada_user.groups.add(contributors)
    permissions = [
        'add_post',
        'change_post',
    ]
    for perm in permissions:
        contributors.permissions.add(
            Permission.objects.get(
                codename=perm,
                content_type__app_label='blog'))


def remove_user_data(apps, schema_editor):
    Group = apps.get_model('auth', 'Group')
    Profile = apps.get_model('user', 'Profile')
    User = apps.get_model('user', 'User')
    Profile.objects.get(slug='andrew').delete()
    Profile.objects.get(
        slug='the_countess').delete()
    User.objects.get(
        email='<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4c28262d222b230c262d212e23223f3b622f2321" rel="noreferrer noopener nofollow">[email protected]</a>').delete()
    User.objects.get(
        email='<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b9d8ddd8f9ddd3d8d7ded6ded0cbd5ca97d6cbde" rel="noreferrer noopener nofollow">[email protected]</a>').delete()
    Group.objects.get(
        name='contributors').delete()


class Migration(migrations.Migration):

    dependencies = [
        ('blog', '0005_post_permissions'),
        ('user', '0002_profile'),
    ]

    operations = [
        migrations.RunPython(
            add_user_data,
            remove_user_data)
    ]

我遇到了这个错误:

AttributeError: 'StateApps' object has no attribute 'label'

我真的不知道“StateApps”是做什么的。但正如错误消息所述,它与分配权限有关。有人可以帮忙吗?该代码来自使用 Django 1.8 的教程,因此我认为是 django 1.11 导致了代码错误。

最佳答案

更改 blog/migrations/0005_post_permissions.py 中的 generate_permissions 函数:

  def generate_permissions(apps, schema_editor):
    Permission = apps.get_model('auth', 'Permission')
    try:
        Permission.objects.get(
            codename='add_post',
            content_type__app_label='blog')
    except Permission.DoesNotExist:
        app_config = apps.get_app_config('blog')
        if app_config.models_module is None:
            app_config.models_module = True
            create_permissions(app_config, verbosity=0)
            app_config.models_module = None
        else:
            raise

这对我来说是 django 2.1 的工作

关于django - 运行迁移时“StateApps”对象没有属性 'label',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46316748/

相关文章:

django - 非空唯一字段的默认值应该是什么

django - 如何在一个类 View 中设置多个权限,取决于http请求

python - 如何在django中使用用户身份验证

python - 查询集注释中的动态字段名称

python - Django的manage.py同步数据库错误

django - 检查 Django 单元测试中是否存在权限对象

python - 我的 Django 模型的权限

python - django 不解析 {{ title }}

django - 在 Django 中创建模型之前如何在 postgresql 中安装扩展?

python - 将 django 模型移动到另一个应用程序,该应用程序是另一个模型的父模型