Nick Dong
Nick Dong

Reputation: 3736

how to add permission to a new model in a program in django 1.5

The target server is running django 1.5 which I could not update. Database is changed by dba which I can not change. I can create a model for new table created by dba on database server. I want to use admin model to manage it. I give a link like /admin/mynewapp/mynewmodel/, but I was told that I dont have permission to operation the model. I can view auth_permission table, and there is no default permission like 'change', 'add' and 'delete' for my new model. There are also no contenttype for my new model. I dont want to use command like syncdb, since I am not sure whether it would make some data loss. I think that the reason that I dont have permission might be there is no default permissions in auth_permission table. Well how to add those default permissions when I create the model without using command syncdb? Thanks.

Upvotes: 0

Views: 1244

Answers (1)

delpierofan
delpierofan

Reputation: 311

First try to use this command line utility provided in Django:

https://github.com/django/django/blob/1.6.5/django/contrib/contenttypes/management.py

to fill up content types table (which should be somewhere over there also). After that you can try to fill up permission table with default ones for all of your apps (installed in INSTALLED_APPS in your settings.py). You can us utility function create_permissions from here:

https://github.com/django/django/blob/1.5/django/contrib/auth/management/init.py

It would be something like:

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

Upvotes: 1

Related Questions