Django manage.py - Creating auth_permission and django_content_type tables
Asked Answered
C

3

6

I am unable to use syncdb because my app uses some MySQL views. I have run manage.py sqlall <app>, but this does not output the SQL for django_content_type table or the auth_permission tables. I have also had a look into south and django evolution, but they both require syncdb, and I'm not sure they would help anyway.

I have manually added some models to the tables, but this is getting frustrating, and having installed the dbsettings app I am unsure of what I now need to enter.

Does anyone know of a way to get manage.py (or something else) to output the SQL for these tables and their contents?

Thanks.

Condescension answered 19/1, 2012 at 15:0 Comment(2)
What is the exact stacktrace?Kymberlykymograph
The last line is _mysql_exceptions.OperationalError: (1060, "Duplicate column name 'ServerID'") - it's something to do with some odd views in MySQL. I've given up on trying to get syncdb working, if I start another django project from scratch I'll use south or evolution.Condescension
C
13

Having done a bit more digging, I found these: Fixing the auth_permission table after renaming a model in Django and manage.py sql command for django models - Django.

These output the tables, but not the data:

python manage.py sql auth
python manage.py sql admin

But this gets a lot closer. In the end I managed it with the following:

from django.contrib.auth.management import create_permissions
from django.db.models import get_apps
for app in get_apps():
    create_permissions(app, None, 2)

from django.contrib.contenttypes.management import update_all_contenttypes
update_all_contenttypes(interactive=True)

This adds all the permissions and then all the content types which are needed. interactive=True means that it asks you if you want to remove stale content types.

Condescension answered 20/1, 2012 at 10:32 Comment(2)
What file does this belong in?Whilom
It's a while since I did this, but I think I was just using the interactive console.Condescension
L
2

@hajamie solution works for older supported version, taking a hint, below is what worked for me!

django = 1.9.7

from django.contrib.auth.management import create_permissions
from django.contrib.auth.models import Permission
from django.apps import apps
def fix_user_permission():
    """
    run this method via shell whenever any amendments in any of the tables is made
    """
    print "fixing user permissions"
     # delete pre-existing user permission 
    Permission.objects.all().delete()
    apps.models_module = True
    create_permissions(apps, verbosity=0)
    apps.models_module = None
    print "process completed - fixed user permissions"
Lillianalillie answered 9/2, 2017 at 11:10 Comment(2)
In Django 1.11 this throws Exception: app_label = app_config.label \n AttributeError: 'Apps' object has no attribute 'label'Ensue
This worked for me in Django 1.11: stackoverflow.com/a/40092780Brough
A
2

The easiest solution I found is to install Django Extensions, add it to settings.INSTALLED_APPS and run:

manage.py update_permissions
Ardie answered 14/10, 2019 at 20:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.