Our post on routers ended up with all four tables wired up - one router doing the URL work for Purchase, plain path() calls for the rest. What we showed the whole time: every one of those URLs is wide open. Anyone with a valid login can hit DELETE /api/purchases/45/ just as easily as GET /api/purchases/45/. That’s fine until it isn’t - say, until the business rule becomes ’employees can look, only managers can delete’. So, in this post, we will set the rules straight - employees can view the tables but only managers could modify them. Let’s get started.

Enforcing a rule by hand

The naive fix is obvious enough: check the user’s group inside the view:

#purchases/views.py handwritten version

def destroy(self, request, *args, **kwargs):
    if not request.user.groups.filter(name='Managers').exists():
        return Response(status=403)
    return super().destroy(request, *args, **kwargs)

One method, one check, looks harmless. The problem shows up once the rule has to apply in more than one place. ShippingStatusView needs the same “managers only” rule, but it’s a completely different class, in a different app - so the check gets copied there too. PurchaseViewSet.update() needs it. partial_update() needs it. Each one is a fresh chance to paste it wrong, forget it, or - worse - only remember it for destroy and quietly leave update open, which is exactly the kind of gap that doesn’t announce itself until someone finds it.

The actual failure mode isn’t ’the check is hard to write’. It’s that authorization logic scattered across method bodies is authorization logic nobody can audit at a glance - you would have to read every view to know what’s actually protected.

What DRF replaces that with

DRF’s answer is permission_classes - a list of classes checked before your view method ever runs. If any of them say no - the request never reaches your code at all; DRF returns 403 automatically. No if buried in destroy() anymore - it’s declared on the class, in one place, visible without reading the method bodies.

For the simple cases, that’s the whole story. CountryListView and PurchasedItemDetailView don’t need role-based logic, just “must be logged in”:

#countries/views.py 

class CountryListView(generics.ListAPIView):
    permission_classes=[IsAuthenticated]
# shipping/views.py

class ShippingStatusView(APIView):
    permission_classes=[IsAuthenticated, IsManager]
# purchases/permissions.py

class IsManager(permissions.BasePermission):
    def has_permission(self, request, view):
        return request.user.groups.filter(name='Managers').exists()

ShippingStatusView is almost as simple, just a stricter rule - every request needs to be manager, no exceptions. That IsManager isn’t built into DRF - IsAuthenticated, IsAdminUser, and a handful of others ship with the framework, but “is this user in the Managers group” is specific to our app, so we need to write it by ourselves.

The method, has_permission(), returning True or False. DRF calls it for you; you never call it yourself.

Where one permission_classes list isn’t enough

PurchaseViewSet is the case that breaks the simple pattern - because it’s not one action, it’s six(list, retrieve, create, update, partial_update, destroy, plus refunds), and they don’t all need the same rule. permissions_classes alone can’t express “viewing is open, everything else needs a manager” - it’s one list, applied uniformly to the whole class.

get_permissions is the escape hatch - instead of a static list, a method that runs per-request and decides which permissions apply, based on self.action

#purchases/views.py 

def get_permissions(self):
    if self.action in ('list', 'retrieve'):
        return [IsAuthenticated()]  
    return [IsAuthenticated(), IsManager()] 

Same ViewSet, same URLs, same router wiring from last time - but now GET /api/purchases/ and DELETE /api/purchases/45/ enforce genuinely different rules, decided in one place instead of scattered across method bodies.

Proof: same URL, different outcome depending on who’s asking

Talk is cheap - here’s the exact same refunds action, hit with the exact same request, by two different users. Nothing changes except who’s logged in.

An employee - authenticated, but not in the Managers group - gets turned away before the method body ever runs:

A manager, same URL, same POST, succeeds:

ShippingStatusView tells the same story, just with no exceptions at all - every action on this view needs IsManager, not just the write ones. An employee is rejected outright, even on a plain GET:

A manager gets the actual data:

Worth being precise about what “employees can only view” actually means, though - it’s a rule that lives on Purchase specifically, not a blanket rule about employees. PurchasedItemDetailView only has IsAuthenticated, no IsManager anywhere in it, so an employee can update one without any special permission at all:

Same mechanism, same has_permission() check, applied - or not applied - per view, per action. That’s the whole point of it living in get_permissions() instead of being hardcoded once for the whole app.

Where this leaves us

Worth being honest about the title here: this post is almost entirely about permissions, not authentication itself - we never actually looked at how SessionAuthentication works under the hood, we just used it as-is. What we did build is the part that actually decides what a logged-in user is allowed to do once they’re recognized: one custom IsManager class, reused everywhere the same rule applies, and get_permissions() for the one view where “everywhere” isn’t quite true. Same shape as every mechanism in this series so far - not magic, just DRF giving you a declared, auditable place to put a check you’d otherwise be tempted to bury inside a method body and hope you remembered everywhere it mattered.

What’s still open: none of this checks which purchase a manager is touching, only that they’re a manager - has_permission(), not has_object_permission(). For four tables with no per-row ownership rules, that distinction hasn’t mattered yet. It will, the day it does.