From fc4c20078c253adffb0f5103dec5a01caba0ac4a Mon Sep 17 00:00:00 2001 From: Ali hassan Date: Sun, 29 Jun 2025 00:33:06 +0500 Subject: [PATCH 1/2] Fix : Updated documentation in tutorial 5 leading to error --- docs/tutorial/5-relationships-and-hyperlinked-apis.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/docs/tutorial/5-relationships-and-hyperlinked-apis.md b/docs/tutorial/5-relationships-and-hyperlinked-apis.md index f999fdf507..8de5b1b7b2 100644 --- a/docs/tutorial/5-relationships-and-hyperlinked-apis.md +++ b/docs/tutorial/5-relationships-and-hyperlinked-apis.md @@ -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. +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. From 6883434b93b5516228616153b8c1be5395a0b17b Mon Sep 17 00:00:00 2001 From: Ali Hassan <124016531+alihassancods@users.noreply.github.com> Date: Wed, 2 Jul 2025 23:15:02 +0500 Subject: [PATCH 2/2] Updated docs/tutorial/5-relationships-and-hyperlinked-apis.md Co-authored-by: Bruno Alla --- .../5-relationships-and-hyperlinked-apis.md | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/docs/tutorial/5-relationships-and-hyperlinked-apis.md b/docs/tutorial/5-relationships-and-hyperlinked-apis.md index 8de5b1b7b2..0c8f372863 100644 --- a/docs/tutorial/5-relationships-and-hyperlinked-apis.md +++ b/docs/tutorial/5-relationships-and-hyperlinked-apis.md @@ -94,16 +94,21 @@ 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. -For example, instead of: +--- + +**Note:** + +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. For example, instead of: serializer = SnippetSerializer(snippet) -you must write: +You must write: serializer = SnippetSerializer(snippet, context={'request': request}) +If your view is a subclass of `GenericAPIView`, you may use the `get_serializer_context()` as a convenience method. + +--- ## 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.