The Python Oracle

Where to control permission-style viewing in Django? In the url (via generic views), template, or view?

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Hypnotic Puzzle3

--

Chapters
00:00 Where To Control Permission-Style Viewing In Django? In The Url (Via Generic Views), Template, Or Vi
01:52 Accepted Answer Score 2
02:06 Thank you

--

Full question
https://stackoverflow.com/questions/1006...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #django #permissions #djangotemplates #djangoviews

#avk47



ACCEPTED ANSWER

Score 2


If you use ListView there is get_queryset() method to do this:

class OrderListView(ListView):
    template_name = 'doors/orders/list.html'

    def get_queryset(self):
        user = self.request.user
        if user.user_type == 1:
            return Order.objects.all()
        return Order.objects.filter(creator=user.pk)