Add note to tutorial about required request in serializer context when using HyperlinkedModelSerializer#9732
Conversation
| 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. |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Shouldn't we write this in the documentation that when using APIView instead of GenericAPIView you should pass the context to avoid any errors.
There was a problem hiding this comment.
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
|
|
||
| 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:** |
There was a problem hiding this comment.
These kind of paragraphs are usually **Note:** and wrapped with horizontal lines (---) before and after.
There was a problem hiding this comment.
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 <[email protected]>
HyperlinkedModelSerializer
|
@browniebroke The changes are approved but not merged yet. Is there specific reason behind this? |
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:
Motivation:
This omission frequently confuses beginners and is a common source of errors. Including this note will:
Fix #9729