Document that read_only_fields ignores explicitly declared fields - #10028
Merged
browniebroke merged 2 commits intoSep 1, 2026
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Updates the DRF serializer documentation to explicitly call out that Meta.read_only_fields does not override attributes for fields that are explicitly declared on the serializer (because the shortcut is applied via extra_kwargs, which declared fields bypass).
Changes:
- Adds an explicit warning under the
read_only_fieldsdocumentation explaining the ignore behavior for explicitly declared fields. - Adds a short cross-reference in the
extra_kwargssection noting that the same rule applies toread_only_fields.
Suppressed comments (1)
docs/api-guide/serializers.md:596
- Similar to the
read_only_fieldsnote above, this sentence could be interpreted asextra_kwargsbeing ignored entirely rather than ignored for the explicitly declared field. Clarifying the scope (and that inherited declared fields also bypassextra_kwargs) would reduce confusion.
Please keep in mind that, if the field has already been explicitly declared on the serializer class, then the `extra_kwargs` option will be ignored. The same is true of `read_only_fields`, which is implemented using `extra_kwargs`.
馃挕 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
browniebroke
approved these changes
Sep 1, 2026
read_only_fields ignores explicitly declared fields
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
refs #3460
read_only_fieldsis implemented by merging intoMeta.extra_kwargs, and explicitly declared fields skip that path. The extra_kwargs docs already mention the ignore behavior; theread_only_fieldssection did not.This adds the same warning next to
read_only_fields, and a one-line cross-reference under extra_kwargs.Maintainers previously asked for this note rather than changing the API:
#3460 (comment)
I also commented on the issue:
#3460 (comment)