how to set the default permission name created in

2019-07-27 22:41发布

问题:

I'm using django 1.6

Now when I define a model, it will create three permissions record for it (can_create, can_update, can_delete).

I'm now adding other permissions on the models (which doesn't matter in this question), and want to make a view to let the user assign them all to users and groups.


Now the problem is:

I want to replace the default name displayed for the three default created permissions.

Is there any way to do this?

回答1:

Based on this blog post and this Django ticket, I would say it is not possible and also not advisable to change these codenames (since they are used in the admin). It is however possible to change the human readable name (such as 'Can add permission')



回答2:

Yes there is possibility to create custom permission while creating the models/table in django. But this will create the extra custom permission, by default 3 permission will create i.e( add, change, delete). One can create custom permission by following thing.

class Task(models.Model):
...
  class Meta:
    permissions = (
        ("view_task", "Can see available tasks"),
        ("change_task_status", "Can change the status of tasks"),
        ("close_task", "Can remove a task by setting its status as closed"),
    )

The only thing this does is create those extra permissions when you run manage.py migrate (the function that creates permissions is connected to the post_migrate signal). Your code is in charge of checking the value of these permissions when a user is trying to access the functionality provided by the application (viewing tasks, changing the status of tasks, closing tasks.) Continuing the above example, the following checks if a user may view tasks:

user.has_perm('app.view_task')

One can see the django doc here django permission description



回答3:

I cannot add comment to the answers already there, hence adding a new answer.

I have been looking for a solution and could not find any. So I just make use of default permissions and use permissions to re-create them and use whatever name and codename you want. Django documentation here

class Foo(models.Model):
    title = models.CharField(max_length=250)

    class Meta:
        default_permissions = ()
        permissions = (
            ("add_foo", "Can add foo"),
            ("change_foo", "Can change foo"),
            ("delete_foo", "Can delete foo"),
            ("view_foo", "Can view foo"),
            ("list_foo", "Can list all foo")
        )