How to only sync custom permissions with syncdb?
Is it possible to have the manage.py syncdb command ONLY sync custom permissions to the auth_permission
table? I don't want the default three permissions installed for the app models, i.e. foo.add_bar
, foo.change_bar
, foo.delete_bar
. I specify my custom 开发者_运维技巧permissions in my model classes like the django documentation suggests:
class Meta:
permissions = (
("admin", "Can create/edit/delete projects"),
("user", "Can view projects"),
)
Thank you!
You have 2 solutions here, AFAIK :
- manually insert in the
auth_permission
table ; - Add in a command this snippet.
Adding a command is pretty straightforward. Just create a module sync_perms.py
(with the snippet code) in a management.commands
package in one of your apps (here the official documentation).
Then run :
python manage.py sync_perms
Et voilà !
Just posted the solution to Remove (or hide) default Permissions from Django. The idea is not to prevent them from being created by syncdb, but to hide them from the admin interface.
first you must find the content type id for your application the content type can be found with this query
select * from django_content_type;
then you execute the query to add to database the custom permission you want for example
insert into auth_permission(name,content_type_id,codename) values
('Can add name',12,'can_add_name');
in the above query , name is a string which the user see in permissions list , the number is the application id found from the first query and codename is the code that you will use in templates for example in template you can use
{{perms.application.can_add_name}}
You need to specify default_permissions
on Meta to be empty tuple. (default value is ('add', 'change', 'delete')
). Docs.
class MyModel(models.Model):
# properties definition...
class Meta:
permissions = (
("admin", "Can create/edit/delete projects"),
("user", "Can view projects"),
)
default_permissions = ()
I also like to have special MyAppProperties
model with Meta.managed=False
to hold my app-wide permissions.
class MyAppPermissions(models.Model):
# properties definition...
class Meta:
managed = False
permissions = (
("admin", "Can create/edit/delete projects"),
("user", "Can view projects"),
)
default_permissions = ()
this way it won't create a database table, but permissions for it. Docs.
精彩评论