Restrict DRF Endpoints: IsAuthenticatedOrReadOnly + Owner Checks
Your DRF snippets API either lets anyone edit anything, or it just rejected a POST with “Authentication credentials were not provided.” Both have the same fix. Three changes give anonymous users read-only access and let only a snippet’s owner edit or delete it.
The short answer
As of November 2026: add permission_classes = [permissions.IsAuthenticatedOrReadOnly] to both SnippetList and SnippetDetail, then override perform_create() on SnippetList to call serializer.save(owner=self.request.user). Anonymous users can then read, and writes need a logged-in user.
That keeps anonymous users out. It doesn’t stop one logged-in user from editing another user’s snippet. For owner-only edits and deletes, also add the custom IsOwnerOrReadOnly permission to SnippetDetail.
What the error means
http POST http://127.0.0.1:8000/snippets/ code="print(123)"
{
"detail": "Authentication credentials were not provided."
}
This is the response from the official DRF tutorial’s snippets API. You get it when you POST a new snippet from an anonymous session, with no login and no credentials attached to the request.
Nothing is wrong with the request itself. It’s well formed. The API refuses it because it can’t tell who’s sending it, and the detail text is the only hint it gives you. Step 3 below explains exactly where the refusal comes from.
The fix, step by step
These steps follow the official DRF tutorial’s Snippet API. They assume Snippet already has an owner field and that SnippetList and SnippetDetail are your two views. The tutorial doesn’t say which DRF or Django versions it was checked against, so there’s no version branching here.
One scope note before you start. This setup only tells anonymous users apart from logged-in users and object owners. Roles like admin or staff are outside what the tutorial covers, so check the DRF permissions docs before relying on it for those.
1. Add a read-only owner field to the serializer
In SnippetSerializer, declare owner as a ReadOnlyField that shows the username:
owner = serializers.ReadOnlyField(source='owner.username')
An untyped ReadOnlyField is used only when serializing output. It’s skipped when a model instance is updated from input, so clients can’t set the owner themselves. Keep it read-only, because that’s the thing stopping a client from claiming someone else’s snippet.
2. Save the owner in perform_create()
This is where the logged-in user gets attached to the new snippet. It’s also the step people skip, because they assume the permission classes handle the owner. They don’t.
A permission only allows or blocks a request and never records who created anything, so perform_create() has to pass the user into save(). Add this method to the SnippetList view class:
def perform_create(self, serializer):
serializer.save(owner=self.request.user)
Without it, obj.owner has nothing meaningful to compare against when the ownership check in step 4 runs.
3. Require login for writes on both views
Import permissions from rest_framework, then add one line to both SnippetList and SnippetDetail:
from rest_framework import permissions
permission_classes = [permissions.IsAuthenticatedOrReadOnly]
IsAuthenticatedOrReadOnly ships with DRF. With it, a logged-in request gets read and write access, and an anonymous one gets read only.
This is also the source of the “Authentication credentials were not provided.” error. If you haven’t configured authentication classes, DRF falls back to its default pair: SessionAuthentication plus BasicAuthentication. An anonymous POST carries no session and no basic credentials for either one to check, so the write is refused.
So why doesn’t this line protect edits on its own? The class is attached to the view, which means the only thing it can sort requests by is anonymous versus logged in. Every logged-in user passes it, whichever snippet they touch.
4. Write the IsOwnerOrReadOnly permission
This one isn’t built into DRF. It’s a custom object-level permission you write yourself in a module inside your app, the same way the tutorial does. Safe methods (GET, HEAD, OPTIONS) pass for every request, so it won’t block anyone from reading another user’s snippet. Any other method passes only when the object’s owner is the requesting user.
from rest_framework import permissions
class IsOwnerOrReadOnly(permissions.BasePermission):
"""
Custom permission to only allow owners of an object to edit it.
"""
def has_object_permission(self, request, view, obj):
# Read permissions are allowed to any request,
# so we'll always allow GET, HEAD or OPTIONS requests.
if request.method in permissions.SAFE_METHODS:
return True
# Write permissions are only allowed to the owner of the snippet.
return obj.owner == request.user
It implements has_object_permission, which receives obj. That’s the only place you can compare obj.owner with request.user. The ownership comparison has to run per object, which is why it lives in a second class instead of on the view.
5. Attach both permissions to SnippetDetail
SnippetDetail works on a single object, so it gets both permissions. Import IsOwnerOrReadOnly from the module where you defined it, then set:

permission_classes = [
permissions.IsAuthenticatedOrReadOnly,
IsOwnerOrReadOnly,
]
Don’t drop IsAuthenticatedOrReadOnly from this list. The owner check only covers who can modify an object. The first class is the one that restricts anonymous users to read-only access, so it handles the anonymous case from step 3 while IsOwnerOrReadOnly adds the per-object ownership check on top.
6. Add the login URLs for the browsable API
To log in through the browsable API, add the api-auth/ route to the project-level urls.py:
from django.urls import path, include
urlpatterns += [
path('api-auth/', include('rest_framework.urls')),
]
The api-auth/ prefix is the one the tutorial uses. Once it’s in place, the browsable API shows a login link, so you can test authenticated requests from the browser. Skip it and your browser requests may stay anonymous, which means writes get “Authentication credentials were not provided.” Add the route and log in before you retry.
What you end up with
| Request | Anonymous | Logged in, not owner | Owner |
|---|---|---|---|
| GET list or detail | Allowed | Allowed | Allowed |
| POST to list | Blocked | Allowed | Allowed |
| PUT/DELETE on detail | Blocked | Blocked | Allowed |
The table follows from the permission logic above. I left out status codes for the blocked cases because the tutorial doesn’t state them.
If all you need is to keep anonymous users to reads, steps 1 through 3 cover the anonymous column. If you need the “Blocked” in the middle of the bottom row, you need steps 4 and 5 too, because nothing at the view level can tell one logged-in user from another.
Sources
This article was created using generative AI based on publicly available sources.