Z
Z
zkweb2016-01-15 16:51:42
Django
zkweb, 2016-01-15 16:51:42

How to allow users to view all posts in the admin panel?

How to allow users to view all posts in the admin panel, BUT not give the right to edit the post.
I'm still learning Django.
I have two user groups.
There are three rules: add_*, change_*, delete_*
As I understand it, in order for users to be able to view all records of a certain model, they need to assign the change_MODEL rule to them, but at the same time they can already edit the record. How can I do better. They need to see everything, but not edit anything)
Thank you!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Nikita Konin, 2016-01-15
@jkjkjirf

Alternatively, you can use the get_readonly_fileds
I.e. despite the fact that the user has rights change_*, he will not be able to edit in fact, because. all fields will be closed for editing.

class BaseAdmin(admin.ModelAdmin):
    def get_readonly_fields(self, request, obj=None):
        if 'имя группы пользователей' in [g.name for g in request.user.groups.all()]:
            return self.get_fields(request, obj)
        return None

Then you can simply inherit all models from BaseAdminin order not to manually override this method.
@admin.register(MyModel)
class MyModelAdmin(BaseAdmin):
    pass

PS To me, my method seems rather crutch, I would be glad to see a more beautiful solution in the answers.

Z
zelsky, 2016-01-15
@zelsky

Create a group with view-only rights, and upon registration, by default, add to this group.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question