| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
|
DRF is considered to be featured complete as per the contributing guidlines. |
Sorry, something went wrong.
…tation - Introduce SerializedRelatedField in rest_framework/relations.py - Supports writing by slug/PK and reading as nested serializer - Add corresponding tests in tests/test_relations.py
| Back | FazBrowse Home | New Git URL |
Add SerializedRelatedField for write-by-slug, read-as-nested representation
Description
Currently, SlugRelatedField only returns the slug value (e.g. a PK or custom field).
Nested serializers, on the other hand, expand the related object but require verbose input payloads.
In many APIs, developers want a hybrid behavior:
This PR introduces SerializedRelatedField, which:
Example
Request
{ "address": 5 }Response
{ "address": { "id": 5, "province": "Tehran", "city": "Tehran", "street": "Valiasr", "additional_info": "Test info" } }References
Testing
Tested locally using django.test.TestCase:
Covers both:
💡 Note: This feature provides a more convenient and expressive way to handle related fields in DRF, reducing boilerplate code and improving API readability.