How to create custom permissions for django admin site

Viewed 118

By default when I create a model Django gives permissions like

add/change/delete/view 

If I give any of the above permission to a specific user then that user can view all the objects from that model. But I want to show them only objects linked with foreign keys.

Let's Say,

I have a Book model

class Book(models.Model):
    title = models.CharField(max_length=100)
    author = models.ForeignKey(
        User, on_delete=models.CASCADE
    )

If I want to give authors permission, they can only view their linked books from the Django admin.

How can I do that?

1 Answers

Everything happens in the Django Meta class. Just follow what the Django Documantation says:

class Meta:
        permissions = [
            ("change_task_status", "Can change the status of tasks"),
            ("close_task", "Can remove a task by setting its status as closed"),
        ]

If you want to manipulate the default permission, you follow these documentation explanation an add this to you model.

Related