ModelSerializer custom serializer for custom fields #8046
Unanswered
isik-kaplan
asked this question in
Ideas & Suggestions
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
https://github.com/encode/django-rest-framework/blob/master/rest_framework/serializers.py#L851 explains what should we do, but doesn't explain why should we do it.
Here in https://github.com/encode/django-rest-framework/blob/master/rest_framework/serializers.py#L855 drf defines a mapping for autogenerated serializer to use, and uses it in https://github.com/encode/django-rest-framework/blob/master/rest_framework/serializers.py#L1205.
In the project I'm currently writing we have couple of custom database fields defined for our use case which have their own serializers, but it feels weird to having to explicitly setting those serializers or subclassing the model serializer and override a bunch of methods just so we can automatically add our serializer to our model field.
This of course is not such a big problem, we can write a base serializer and just inherit the model serializer and do something like
so far so good. This takes care of our need to a certain degree. But now, we decided to separate our fields into their own apps and wanted to use the serializer from our libraries with drf. And because drf model serializer doesn't have a
get_serializer_field_mapping
we have to have something ugly likewhile we could've done simply
via the
super().get_serializer_field_mapping()
so that the lib_abc wouldn't care about lib_xyz and vice versa.And it is like only 3-4 lines of code change in DRF's part, hope you consider adding this.
Beta Was this translation helpful? Give feedback.
All reactions