Skip to content

Fixed Documentation : Add clarification about context={'request': request} when using HyperlinkedModelSerializer #9729 #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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions docs/tutorial/5-relationships-and-hyperlinked-apis.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Author

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.

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, the SnippetDetail and SnippetList both inherit GenericAPIView, which has a get_serializer_context() method. Perhaps this would be a good moment to mention this method.

Copy link
Author

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

For example, instead of:

serializer = SnippetSerializer(snippet)

you must write:

serializer = SnippetSerializer(snippet, context={'request': request})

## Making sure our URL patterns are named

If we're going to have a hyperlinked API, we need to make sure we name our URL patterns. Let's take a look at which URL patterns we need to name.
Expand Down
Loading