django-rest-polymorphic icon indicating copy to clipboard operation
django-rest-polymorphic copied to clipboard

Add `child_kwargs` to PolymorphicSerializer

Open NDevox opened this issue 4 years ago • 3 comments

This enables passing in a dictionary param, child_kwargs, to a PolymorphicSerializer. In doing so the child_kwargs will not be applied to the Polymorphic Serializer - but will be applied to the child serializers alongside kwargs.

This resolves issue #33

NDevox avatar Feb 08 '21 12:02 NDevox

Codecov Report

Merging #34 (d78be9a) into master (69923ad) will increase coverage by 0.07%. The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master      #34      +/-   ##
==========================================
+ Coverage   93.10%   93.18%   +0.07%     
==========================================
  Files           1        1              
  Lines          87       88       +1     
==========================================
+ Hits           81       82       +1     
  Misses          6        6              

codecov[bot] avatar Feb 08 '21 12:02 codecov[bot]

@NDevox Hi, thanks for the PR. If I understand correctly, it passes the same child kwargs to every child serializer, correct? What if I want to pass different parameters to different child serializers? Especially, when it comes to drf-flex-fields, it should be very common to want something like that.

denisorehovsky avatar Mar 15 '21 09:03 denisorehovsky

Yep that is the crux of the PR. Interesting idea on having type specific kwargs.

Definitely possible, question is what would be the best API for this? Do we want to do it based on specific object instance? or specific serializer type? My specific usecase for instance would want to do it on serializer type.

Could do something like:

child_kwargs = {
    "SerializerType": serializer_type_kwargs,  # Applies only to SerializerType children
    "*": global_child_kwargs,  # Applies to all children.
}

NDevox avatar Mar 15 '21 10:03 NDevox