JPC
JPC

Reputation: 8276

Django - some permissions are missing in admin tool

I'm not quite sure what to make of this. I'm using Django in eclipse. I made a new django project, and copied the code from a previous django project into this one. I ran syncdb to set up the database. I went to the admin page to recreate my groups, but I noticed that some of the permissions were missing from the list of Available permissions. In the previous project, there was a permission to let me "access" a profile. In this project, that permission isn't on the list, but it's also the permission that I need. Where did it go?

Upvotes: 3

Views: 2393

Answers (2)

Djones4822
Djones4822

Reputation: 767

This question came up on a search for why django was giving me a "Permission not Found" error during a data migration that creates my default permission groups when I was creating a fresh db instance.

For me, the answer was related to how permissions are created during the "post migration signal" step. So when building migrations incrementally, that signal is emitted which creates the permissions. But when rebuilding the database this signal is not emitted until the final migration completes successfully. So if one of your data migrations references permissions then you will get the error I encountered. Specifically, our data model changed significantly during development so we cleared the migrations, copied in the data migrations, and issued migrate

A great discussion is here: https://www.webinative.com/blog/django-permission-not-found-error/

The solution is to force create the permissions prior to whatever data migration needed them.

from django.db import migrations
from django.contrib.auth.management import create_permissions

def force_create_permissions(apps, schema_editor):
    """ creates permissions without waiting for post_migrate signal """
    for app_config in apps.get_app_configs():
        app_config.models_module = True
        create_permissions(app_config, apps=apps, verbosity=0)
        app_config.models_module = None

[ the rest of your data migration logic...]

class Migration(migrations.Migration):

    dependencies = [
        (...),
    ]

    operations = [
        migrations.RunPython(force_create_permissions),
        migrations.RunPython(...)
    ]

Big thanks to the linked blog post and all credit goes to Magesh Ravi (the author)!

To this question, I'm wondering if one of the data migration steps in the original project included this step, but a later one did not, so there were SOME permissions, but not all.

Upvotes: 0

S.Lott
S.Lott

Reputation: 391818

Where did it go?

When you incrementally change your database by dropping tables and running syncdb, the PK of the application table are reflected in the auth_permission table can change.

Don't do "incremental" surgery if you can avoid it.

  1. Extract your data.

  2. Drop your database.

  3. Rerun syncdb to rebuild it.

  4. Reload your data.

You'll be much happier.

Upvotes: 3

Related Questions