Skip to content
Advertisement

Django migrations error: dont create authomatically permissions and contenttypes

I work with django 1.8.7 and python 2.7 . I have peoblem with migrate. when I migrate my models , permission and contenttypes don’t create automatically. error looks like this:

    Traceback (most recent call last):
    File "/home/mariocesar/Proyectos/Crowddeals/env/local/lib/python2.7/site- 
    packages/django/core/management/base.py", line 222, in run_from_argv
    self.execute(*args, **options.__dict__)
    File "/home/mariocesar/Proyectos/Crowddeals/env/local/lib/python2.7/site- 
    packages/django/core/management/base.py", line 252, in execute
    output = self.handle(*args, **options) File"/home/mariocesar/Proyectos/Crowddeals/crowddeals/core/management/commands/update_pe 
    rmissions.py", line 29, in handle
    create_permissions(app, get_models(), options.get('verbosity', 0))
    File "/home/mariocesar/Proyectos/Crowddeals/env/local/lib/python2.7/site- 
    packages/django/contrib/auth/management/__init__.py", line 74, in create_permissions
    for perm in _get_all_permissions(klass._meta, ctype):
    File "/home/mariocesar/Proyectos/Crowddeals/env/local/lib/python2.7/site- 
    packages/django/contrib/auth/management/__init__.py", line 28, in 
    _get_all_permissions
    _check_permission_clashing(custom, builtin, ctype)
    File "/home/mariocesar/Proyectos/Crowddeals/env/local/lib/python2.7/site- 
    packages/django/contrib/auth/management/__init__.py", line 49, in 
    _check_permission_clashing
    for codename, _name in custom:
    ValueError: too many values to unpack

and when I change migrations file like this:

    # -*- coding: utf-8 -*-
    from __future__ import unicode_literals

    from django.contrib.auth.management import _get_builtin_permissions, _check_permission_clashing
    from django.db import migrations, models
    from django.conf import settings
    def _get_all_permissions(opts, ctype):
        """
        Returns (codename, name) for all permissions in the given opts.
        """
        builtin = _get_builtin_permissions(opts)
        custom = list(opts.permissions)

        if any(isinstance(el, basestring) for el in custom):
            custom = [custom]

        _check_permission_clashing(custom, builtin, ctype)
        return builtin + custom


    class Migration(migrations.Migration):

        dependencies = [
            migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ]

        operations = [
            migrations.CreateModel(
                name='JsonModel',
                fields=[
                    ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                    ('data', models.CharField(max_length=1000000)),
                    ('generatedtime', models.DateTimeField(auto_now_add=True, null=True)),
                    ('title', models.CharField(max_length=200, null=True, blank=True)),
                    ('type', models.IntegerField(default=0)),
                    ('user', models.ForeignKey(to=settings.AUTH_USER_MODEL)),
                ],
                options={
                    'permissions': (('view_jsonmodel', 'Can view jsonmodel'), ('view_jsonmodel_int', 'Can view jsonmodel_int')
                },
            ),
        ]

        def forwards(self, orm):
            from django.contrib.auth import management
            management._get_all_permissions = _get_all_permissions

and run command ” python manage.py migrate ” this is responsed:

        Operations to perform:
    Apply all migrations: (none)
    Running migrations:
    No migrations to apply.
    Your models have changes that are not yet reflected in a migration, and so won't be applied.
    Run 'manage.py makemigrations' to make new migrations, and then re-run 'manage.py migrate' to apply them.
    Traceback (most recent call last):
    File "manage.py", line 29, in <module>
        execute_from_command_line(sys.argv)
    File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 354, in execute_from_command_line
        utility.execute()
    File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 346, in execute
        self.fetch_command(subcommand).run_from_argv(self.argv)
    File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 394, in run_from_argv
        self.execute(*args, **cmd_options)
    File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 445, in execute
        output = self.handle(*args, **options)
    File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/migrate.py", line 226, in handle
        emit_post_migrate_signal(created_models, self.verbosity, self.interactive, connection.alias)
    File "/usr/local/lib/python2.7/dist-packages/django/core/management/sql.py", line 280, in emit_post_migrate_signal
        using=db)
    File "/usr/local/lib/python2.7/dist-packages/django/dispatch/dispatcher.py", line 189, in send
        response = receiver(signal=self, sender=sender, **named)
    File "/usr/local/lib/python2.7/dist-packages/django/contrib/auth/management/__init__.py", line 84, in create_permissions
        for perm in _get_all_permissions(klass._meta, ctype):
    File "/usr/local/lib/python2.7/dist-packages/django/contrib/auth/management/__init__.py", line 24, in _get_all_permissions
        _check_permission_clashing(custom, builtin, ctype)
    File "/usr/local/lib/python2.7/dist-packages/django/contrib/auth/management/__init__.py", line 47, in _check_permission_clashing
        for codename, _name in custom:

thanks for your help. :)

Advertisement

Answer

I find the solution, in other model.py apps when define custom permissions , my colleagues define custom permissions like this:

    class Meta:
    permissions = (
        ('view_log', 'Can view log'),
        ('view_failedloginlog', 'Can view failed login log'),
        ('view_loginlog', 'Can view login log'),
        ('view_loginattempt', 'Can view login attempt'),
        ('view_onlineusers', 'Can view online users')
    )

although, type of permission should be tupe; this means permission should be define like this:

    class Meta:
    permissions = (
        ('view_log', 'Can view log'),
        ('view_failedloginlog', 'Can view failed login log'),
        ('view_loginlog', 'Can view login log'),
        ('view_loginattempt', 'Can view login attempt'),
        ('view_onlineusers', 'Can view online users'),
    )

this “,” is so important!!! :))

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement