Support structural changes between source model and serialized object #8782
-
Note: this discussion stems from this comment. Problem statementI've bumped into the following limitation while trying to implement a serializer which represents a restructured view from multiple models: class Person:
name: str
cars: CarStats
stocks: StockStats
class WealthSerializer(serializers.Serializer):
cars = serializers.FloatField(source='cars.total_price')
stocks = serializers.FloatField(source='stocks.total_value')
class CarsSerializer(serializers.Serializer):
total_cars = serializers.IntegerField(source='cars.count')
oldest_car_date = serializers.DateField(source='cars.oldest_date')
class PersonSerializer(serializers.Serializer):
name = serializers.CharField()
wealth = WealthSerializer(source='*') # use * as this serializer uses multiple Person fields
cars = CarsSerializer(source='*') # use * as this serializer uses multiple Person fields Just like in the original issue description, Conceptually speaking, this problem falls into the "Implementation details leakage"[1][2], as the serializer (by not supporting this use case) is coupling the endpoint schema with the internal model schema. Proposed solution
Even though this limitation prevents some good practices for developing loosely coupled interfaces, I agree that supporting yet another corner case (i.e., first time bumping into this limitation in years) makes increases the potential for user error, hence my solution was to:
Note: While point 1. is mandatory in my proposal, point 2. can be implemented by the actual framework users ad-hoc. DiscussionHere's some pros/cons of the approach to jumpstart the discussion: Pros:
Cons:
WDYT? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
Thanks for this @JPDSousa. As per my comment on #6345:
More or less, so few people need this that for me it's not worth the overhead of adding it. Even a hook would need documentation, and would force each user to understand it, and decide whether or not it would apply to them, which almost certainly it wouldn't That, prior to your comment, the issue was dormant since 2018 tells us what we need to know. 🙂 |
Beta Was this translation helpful? Give feedback.
-
It looks like we don't share the same opinions on what this means. 🙂 It's not that I think adding such a hook is not worth, rather I think it would be positively harmful, because it would increase the overall cognitive load of using DRF in order to serve only a most esoteric edge case. In my opinion that would be bad practice. All I can suggest is that you take a look at John Ousterhout's A Philosophy of Software Design, which is perhaps the best thing I can think of that approaches these topics in the manner I've come to think of as leading to the software that best serves the needs of its users. |
Beta Was this translation helpful? Give feedback.
Thanks for this @JPDSousa.
As per my comment on #6345:
More or less, so few people need this that for me it's not worth the overhead of adding it. Even a hook would need documentation, and would force each user to understand it, and decide whether or not it would apply to them, which almost certainly it wouldn't
That, prior to your comment, the issue was dormant since 2018 tells us what we need to know. 🙂