Ensure DynamicRelationField.to_representation is passed correct instance #297
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This fixes a regression that was originally introduced in d0c31d3 and a869d28
There were a few places in our code where we have subclassed
DynamicRelationField
and overriddento_representation
, conditionally short-circuiting the serialization of a many to many field depending on the request, context, or a property of the parent instance. We had been relying on the fact thatto_representation
for aModelField
and aDynamicRelationField
was always passed the parent instance. But in d0c31d3, and then a869d28, some of the responsibilities ofto_representation
were moved over toget_attribute
.After updating to the latest version our methods no longer worked, because
to_representation
was now being passed theManyRelatedManager
, not the parent instance. This seems to me like a bug, as it breaks expectations about the arguments and return values ofget_attribute
andto_representation
. And so far as I can tell, moving the logic back fromget_attribute
doesn't break anything in dynamic-rest, since the two methods are only ever called together, e.g.: inWithDynamicSerializerMixin._faster_to_representation()
:I've included a test that shows roughly how we sometimes override
to_representation
.