Feature: URL Parameter to specify Serialization depth
DRF allows you to specify depth under Meta. Defaults to 1 so PK is shown in serialized JSON instead of nested object. I think this should be a parameter and maybe we can do it dynamically?
It's not quite as simple as depth because it also comes down to the way the nested objects are serialized. Additionally, the implications of the related nested queries and the indexing must be considered.
In any case, it's a great idea, but it will likely be a little more involved than we think. It's worth some experimentation.
This might be useful advice: https://medium.com/@eduardosilva/django-rest-framework-why-you-should-avoid-the-depth-option-f6adbf1b24#.jjst2k8aa
I'll continue to investigate
https://docs.djangoproject.com/en/dev/ref/models/querysets/#select-related
This seems pretty feasible, though I wish it was less explicit. We could generate the viewset queryset, adding select_related for everything in:
.select_related(*[i.replace('_id', '') for i in self.queryset.model._meta.get_all_field_names() if '_id' in i])
...or define it manually per viewset which would probably be more predictable and just have a single flag to turn the knob on.
Yeah .select_related() is a good choice, but I would want those serializers to be used very carefully and explicitly because that has a notable performance impact, especially on very large query sets.
I've started reviewing the reality of this and came across "DRF serializer extensions". It has some really cool stuff built that is more highly optimized than the default DRF "depth" stuff.
See: http://django-rest-framework-serializer-extensions.readthedocs.io/en/latest/usage-serializers/
Source: https://github.com/evenicoulddoit/django-rest-framework-serializer-extensions
So far in my initial research my favorite is "DRF serializer extensions", but here are some other's I've been tracking for reference. I'm posting these here because I'm sick of keeping the browser tabs open:
- https://github.com/AltSchool/dynamic-rest
- https://github.com/dbrgn/drf-dynamic-fields
- https://github.com/beda-software/drf-writable-nested
- https://github.com/rsinger86/drf-flex-fields
- https://github.com/wq/django-natural-keys
This particular pattern for deeply nesting serialization is also worth looking at: https://machinalis.com/blog/nested-resources-with-django/