R
R
raiboon2015-01-31 16:10:07
Django
raiboon, 2015-01-31 16:10:07

How to manage permissions in django?

For example, I need the permissions-filtering view functionality. There is no such thing in django.
How to implement it?
Examples. There is an author, he can view all published materials and all his own - incl. unpublished and non-public materials.
An unauthorized person can only view publicly posted content.
Admins can view absolutely any posts.
You can shove this logic into all views and tags. But it turns out code duplication. You can inherit from a view with get_context_data overridden. Already better. But still, you can be wrong. But again, duplication can be found in some template tag - for example, random topics in the sidebar.
The most beautiful option I see is to redefine the objects of the model. That's very beautiful. But without crutches, it will not be possible to get the user in the insides of the model.
How right?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ilya, 2015-01-31
@FireGM

You can inherit from a view with get_context_data overridden.

Apparently you are using CBV. Just make a mixin with get_queryset overridden. Add filtering there with Q if the user is authorized.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question