5

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.

hajamie
  • 2,848
  • 2
  • 22
  • 20
  • What is the exact stacktrace? – armonge Jan 19 '12 at 16:34
  • 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. – hajamie Jan 19 '12 at 17:50

3 Answers3

12

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.

Community
  • 1
  • 1
hajamie
  • 2,848
  • 2
  • 22
  • 20
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"
NoobEditor
  • 15,563
  • 19
  • 81
  • 112
  • In Django 1.11 this throws Exception: `app_label = app_config.label ` \n `AttributeError: 'Apps' object has no attribute 'label'` – kunambi May 11 '17 at 05:24
  • 1
    This worked for me in Django 1.11: https://stackoverflow.com/a/40092780 – Crag Jul 06 '17 at 22:03
2

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

manage.py update_permissions
cethegeek
  • 6,286
  • 35
  • 42