[Django]-How to only sync custom permissions with syncdb?

10👍

You have 2 solutions here, AFAIK :

  1. manually insert in the auth_permission table ;
  2. 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à !

👤Stan

1👍

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.

1👍

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}}

0👍

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.

Leave a comment