2

As I am trying to run the this migration:

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)
    ]

I ran in this error:

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

I don't really know what the 'StateApps' does. But As the error message states it has something to do with assigning the permissions. Can someone help? This code is from a tutorial using Django 1.8 so I think that it is django 1.11 which is making the code error.

Xen_mar
  • 8,330
  • 11
  • 51
  • 74
  • Possible duplicate of [How to add a permission to a user/group during a django migration?](https://stackoverflow.com/questions/38822273/how-to-add-a-permission-to-a-user-group-during-a-django-migration) – gonczor Nov 30 '17 at 12:41

1 Answers1

2

Change generate_permissions function in blog/migrations/0005_post_permissions.py:

  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

It's work for me with django 2.1

varga
  • 21
  • 3