Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django manage.py - Creating auth_permission and django_content_type tables

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.

like image 944
hajamie Avatar asked Jan 19 '12 15:01

hajamie


3 Answers

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.

like image 154
hajamie Avatar answered Nov 05 '22 16:11

hajamie


@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"
like image 45
NoobEditor Avatar answered Nov 05 '22 15:11

NoobEditor


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

manage.py update_permissions
like image 2
cethegeek Avatar answered Nov 05 '22 16:11

cethegeek