Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

@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"

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

manage.py update_permissions
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top