You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have this custom serializer field that is used in a number of serializers:
classNestedPrimaryKeyRelatedField(serializers.PrimaryKeyRelatedField):
def__init__(self, serializer, **kwargs):
""" On read display a complete nested representation of the object(s) On write only require the PK (not an entire object) as value """self.serializer=serializersuper().__init__(**kwargs)
defto_representation(self, obj):
returnself.serializer(obj, context=self.context).to_representation(obj)
# UsageclassMySerializer:
related_obj=NestedPrimaryKeyRelatedField(RelatedSerializer, allow_null=True, required=False)
The idea is that when the client GETs MySerializer they receive a nice nested representation of related_obj using RelatedSerializer, but when they POST/PUT/PATCH they only need to provide the PK (not an entire object) to set the value of related_obj.
The actual functionality works as expected, but the schema generated by Spectacular assumes the field is just a primary key for both read and write operations, while in reality on read the schema should be a full object based on RelatedSerializer.
I tried to create a custom extension but I'm struggling with the fine details:
classNestedPkExtension(OpenApiSerializerFieldExtension):
# Ensure annotations use different read/write serializers when using NestedPrimaryKeyRelatedFieldtarget_class=NestedPrimaryKeyRelatedFielddefmap_serializer_field(self, auto_schema, direction: Direction):
# I know the direction plays a role here, but don't know exactly whatifdirection=="response":
# Return an object schemaelse:
# Return a primary key schema
Any help would be appreciated 🙏
The text was updated successfully, but these errors were encountered:
Please be aware that asymmetric serializers requires you to use the setting 'COMPONENT_SPLIT_REQUEST': True. OpenAPI by itself cannot represent request/response differences. We therefore split them in two component.
I think the following should do whay you need. they key is to know the where to hook into auto_schema.
classNestedPkExtension(OpenApiSerializerFieldExtension):
# Ensure annotations use different read/write serializers when using NestedPrimaryKeyRelatedFieldtarget_class=NestedPrimaryKeyRelatedFielddefmap_serializer_field(self, auto_schema, direction: Direction):
ifdirection=="response":
# target is NestedPrimaryKeyRelatedField instance.# build a component from the serializer and return a reference to that componentcomponent=auto_schema.resolve_serializer(self.target.serializer, direction)
returncomponent.refifcomponentelseNoneelse:
# Return a primary key schemareturnbuild_basic_type(OpenApiTypes.UUID) # or whatever key you use
I have this custom serializer field that is used in a number of serializers:
The idea is that when the client GETs
MySerializer
they receive a nice nested representation ofrelated_obj
usingRelatedSerializer
, but when they POST/PUT/PATCH they only need to provide the PK (not an entire object) to set the value ofrelated_obj
.The actual functionality works as expected, but the schema generated by Spectacular assumes the field is just a primary key for both read and write operations, while in reality on read the schema should be a full object based on
RelatedSerializer
.I tried to create a custom extension but I'm struggling with the fine details:
Any help would be appreciated 🙏
The text was updated successfully, but these errors were encountered: