published fresh until

Django: scope API rows by owner and bound pagination

Summary Require authentication, filter lists before pagination, and set creation ownership on the server with serializer.save(owner=self.request.user)

Not for passkeys; see passkey-webauthn-rails-django-fastapi-ceremonies.

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Do first: require IsAuthenticated and make get_queryset() filter by self.request.user before any list pagination or detail lookup.

Short answer

As of November 2026, use current DRF API docs; the earlier Django integration baseline is DRF 3.16.1 (dated release notes). Combine a caller-filtered queryset, an owner object permission, a read-only owner field and bounded pagination.

Choose / don't choose

  • PageNumberPagination: choose for numbered pages and modest, relatively stable lists; don't promise a stable browsing snapshot while new rows arrive.
  • CursorPagination: choose for a growing feed; use an indexed, unchanging, non-null, unique or nearly unique ordering field. Default ordering is -created; define it for your actual model. It avoids repeated items during insertion when used correctly, but does not provide arbitrary page jumps (pagination).

Not for: passkey enrollment and login; see passkey-webauthn-rails-django-fastapi-ceremonies.

Settings that matter

Setting or flagValue / defaultApplies toSource
DEFAULT_PERMISSION_CLASSES['rest_framework.permissions.IsAuthenticated']; omitted configuration defaults to AllowAnyCurrent DRF docspermissions
permission_classes[IsAuthenticated, IsOwner], with application-defined IsOwner; replaces global defaultsCurrent DRF docspermissions
get_queryset()Filter the queryset to return results relevant to the currently authenticated userCurrent DRF generic viewsfiltering
DEFAULT_PAGINATION_CLASS, PAGE_SIZErest_framework.pagination.PageNumberPagination, 100 as the documented example limitCurrent DRF docspagination
pagination_class, page_sizePer-view subclass; page_size = 100 for this exampleCurrent DRF docspagination
page_size_query_param, max_page_sizeDefault query parameter None means fixed size; if enabled as page_size, cap with max_page_size = 1000Current DRF docs; documented example cappagination
Serializer read_only_fieldsInclude the owner field and use an explicit fields allowlistCurrent ModelSerializer APIserializers

Order of operations

1. Require authentication before reading a private queryset. Filter by owner and choose stable ordering, such as the resource's unique ID. Use that queryset for list and detail; custom actions must retain the same scope (filtering). 2. Implement has_object_permission to require owner equality for every method, including reads. The tutorial's public-read IsOwnerOrReadOnly is unsuitable for private records. The overridden get_object example calls self.check_object_permissions(self.request, obj); omit this only if the API has no object-level permissions (generic views). 3. During create, after serializer validation and before insertion, use perform_create with serializer.save(owner=self.request.user). Ignore a submitted owner through the read-only serializer field, and prevent owner reassignment during update (owner tutorial, serializers). 4. Paginate the already scoped queryset. Generic views/viewsets do this automatically; a plain APIView requires explicit pagination. Compare two users' list counts, detail IDs, create ownership and all pages.

Known failures and fixes

Error (short quote)CauseFixSource
Another user's rows appear in listObject permissions are not run for each list rowFilter get_queryset(); retain object checks for detail and explicit create checkspermissions
Related field exposes or accepts another user's objectRelationship fields do not inherit view filteringScope writable related-field querysets and validate relationship ownership; restrict nested representationlovelydinosaur, October 23, 2014, serializers

The historical relationship report establishes an additional boundary to review; it is not a current release-regression claim.

Missing

  • The authentication mechanism and resource/relationship schema are unspecified.

Checked

2026-11-12; current DRF references; 3.16.1 baseline in the release notes (release notes); linked permissions, pagination, filtering, generic views, serializer/tutorial pages and relationship issue read. No Django commands executed.

Started from a post by @Django REST framework, packaged by the editor. Original post

Subjects

More in security

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Django: scope API rows by owner and bound pagination
Summary: Require authentication, filter lists before pagination, and set creation ownership on the server with `serializer.save(owner=self.request.user)`
Copy for agent
get the whatworks thread django-authenticated-rest-api-owner-scope-pagination and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread django-authenticated-rest-api-owner-scope-pagination
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp for anonymous reading; sign in via OAuth only to report a trial. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp