Paullo
Paullo

Reputation: 2127

Django 2.1 View Permission

The Django Doc states that the new View Permission is added to Django 2.1 but without further clarifications on how this will be used especially on the Django Admin Site.

It is my understanding this will be kind of ReadOnly Permission, but I will appreciate more clarifications from any one with better understanding on this new permission and its behavior on the Admin Site

Upvotes: 4

Views: 2532

Answers (2)

Algebra
Algebra

Reputation: 316

You are right they didn't clarify on how to use it, but I found out how to use it. By default, the view permission will return true, so its only use case is if you want to prevent users from viewing the admin object. And this is how you can do it.

class ViewAdmin(admin.ModelAdmin):
    
    def has_view_permission(self, request, obj=None):
        return False

Upvotes: 0

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476614

You are correct. The Django 2.1 release notes describe this change:

Model "view" permission

A "view" permission is added to the model Meta.default_permissions. The new permissions will be created automatically when running migrate.

This allows giving users read-only access to models in the admin. ModelAdmin.has_view_permission() is new. The implementation is backwards compatible in that there isn’t a need to assign the "view" permission to allow users who have the "change" permission to edit objects.

Furthermore the documentation for ModelAdmin.has_view_permission [Django-doc] permission explains it like:

ModelAdmin.has_view_permission(request, obj=None)

Should return True if viewing obj is permitted, False otherwise. If obj is None, should return True or False to indicate whether viewing of objects of this type is permitted in general (e.g., False will be interpreted as meaning that the current user is not permitted to view any object of this type).

The default implementation returns True if the user has either the "change" or "view" permission.

So in this case, the "change" permission implies the "view" permission (which is rather logical, since it would be strange to change an object, without being able to see it first).

Upvotes: 4

Related Questions