Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add note to tutorial about required request in serializer context when usingHyperlinkedModelSerializer#9732

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Conversation

alihassancods
Copy link
Contributor

@alihassancodsalihassancods commentedJun 30, 2025
edited by browniebroke
Loading

This PR updates the Tutorial 5 Relationships & Hyperlinked APIs documentation to include an important note:

When using HyperlinkedModelSerializer inside manually instantiated serializers in views (e.g., SnippetDetail), you must pass context={'request': request} to ensure URL fields resolve correctly.

Without this context, developers following the tutorial as written will encounter errors like:

HyperlinkedIdentityField requires the request in the serializer context. Add context={'request': request} when instantiating the serializer.

Motivation:

This omission frequently confuses beginners and is a common source of errors. Including this note will:

  • Reduce friction for new users
  • Prevent unnecessary troubleshooting

Fix#9729

Copy link
Member

@auvipyauvipy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

looks good

@@ -94,6 +94,16 @@ Notice that we've also added a new `'highlight'` field. This field is of the sa

Because we've included format suffixed URLs such as `'.json'`, we also need to indicate on the `highlight` field that any format suffixed hyperlinks it returns should use the `'.html'` suffix.

**Important:**
When you are manually instantiating these serializers inside your views (e.g., in `SnippetDetail` or `SnippetList`), you **must** pass `context={'request': request}` so the serializer knows how to build absolute URLs.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I believe that by this point, theSnippetDetail andSnippetList both inheritGenericAPIView, which has aget_serializer_context() method. Perhaps this would be a good moment to mention this method.

auvipy reacted with thumbs up emoji
Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Shouldn't we write this in the documentation that when using APIView instead of GenericAPIView you should pass the context to avoid any errors.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Yes, perhaps. What I'm suggesting is to add a reminder that if you use one of the generic views, you have a method to build the context data that you should use instead of creating the whole dict literal

@@ -94,6 +94,16 @@ Notice that we've also added a new `'highlight'` field. This field is of the sa

Because we've included format suffixed URLs such as `'.json'`, we also need to indicate on the `highlight` field that any format suffixed hyperlinks it returns should use the `'.html'` suffix.

**Important:**
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

These kind of paragraphs are usually**Note:** and wrapped with horizontal lines (---) before and after.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

These kind of paragraphs are usually**Note:** and wrapped with horizontal lines (---) before and after.

Sure i will take care of this in my next commits.

Co-authored-by: Bruno Alla <browniebroke@users.noreply.github.com>
@browniebrokebrowniebroke changed the titleFixed Documentation : Add clarification about context={'request': request} when using HyperlinkedModelSerializer #9729Add note to tutorial about required request in serializer context when usingHyperlinkedModelSerializerJul 6, 2025
@alihassancods
Copy link
ContributorAuthor

@browniebroke The changes are approved but not merged yet. Is there specific reason behind this?

Copy link
Member

@auvipyauvipy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

lgtm

@browniebrokebrowniebroke merged commit2ae8c11 intoencode:masterJul 7, 2025
7 checks passed
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@browniebrokebrowniebrokebrowniebroke approved these changes

@auvipyauvipyauvipy approved these changes

Assignees
No one assigned
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

Improper Docs in Tutorial 5 causing error when using HyperlinkedModelSerializer
3 participants
@alihassancods@browniebroke@auvipy

[8]ページ先頭

©2009-2025 Movatter.jp