【问题标题】:'StateApps' object has no attribute 'label' while running migration'StateApps' 对象在运行迁移时没有属性'label'
【发布时间】:2017-09-20 08:14:42
【问题描述】:

当我尝试运行此迁移时:

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='django@jambonsw.com',
        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='ada@djangogirls.org',
        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='django@jambonsw.com').delete()
    User.objects.get(
        email='ada@djangogirls.org').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' 对象没有属性 'label'

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

【问题讨论】:

标签: django django-migrations django-permissions


【解决方案1】:

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 对我有用

【讨论】:

    猜你喜欢
    • 2018-02-14
    • 1970-01-01
    • 2022-01-11
    • 2018-10-10
    • 2021-12-15
    • 2015-09-21
    • 2015-06-15
    • 2019-07-06
    • 2022-01-12
    相关资源
    最近更新 更多