Skip to content

Commit 2ae8c11

Browse files
Add note to tutorial about required request in serializer context when using HyperlinkedModelSerializer (#9732)
* Fix : Updated documentation in tutorial 5 leading to error * Updated docs/tutorial/5-relationships-and-hyperlinked-apis.md Co-authored-by: Bruno Alla <browniebroke@users.noreply.github.com> * Missing newline --------- Co-authored-by: Bruno Alla <browniebroke@users.noreply.github.com>
1 parent 70e54f4 commit 2ae8c11

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

docs/tutorial/5-relationships-and-hyperlinked-apis.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,22 @@ Notice that we've also added a new `'highlight'` field. This field is of the sa
9494

9595
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.
9696

97+
---
98+
99+
**Note:**
100+
101+
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:
102+
103+
serializer = SnippetSerializer(snippet)
104+
105+
You must write:
106+
107+
serializer = SnippetSerializer(snippet, context={'request': request})
108+
109+
If your view is a subclass of `GenericAPIView`, you may use the `get_serializer_context()` as a convenience method.
110+
111+
---
112+
97113
## Making sure our URL patterns are named
98114

99115
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.

0 commit comments

Comments
 (0)