[Django]-Django – some permissions are missing in admin tool

3👍

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.

👤S.Lott

0👍

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.

Leave a comment