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
with my poor ability to find references on google, I finally ended up choosing this area for asking problems.
Assuming that I set a custom field using SerializerMethodfield that returns 2 different values by a certain condition as below.
class MySerializer(serializer.ModelSerializer):
product = serializers.SerializerMethodField()
@extend_schema_field(field=OpenApiTypes.STR)
def get_product(self, obj):
if obj.product:
return obj.product.name
return {}
It should return string or empty dict by a condition but All I can set for this was string type with only one value.
Is it technically wrong for the swagger to show different values or I can do more customizing with this?
The text was updated successfully, but these errors were encountered:
As the document says here that overriding OpenApiSerializerFieldExtension is functionally same with adding field attribute in @extend_schema_field
When a customized serializer field given, It comes as Class so I can assign it with target_class,
but SerializerMethodField is defined as a function that normally starts with def get_.
then not sure how I can override OpenApiSerializerFieldExtension for this :(
As the document says here that overriding OpenApiSerializerFieldExtension is functionally same with adding field attribute in @extend_schema_field
yes. it does the same, but depending on what you need you have slightly different options.
You don't need OpenApiSerializerFieldExtension here. It is meant for custom fields. SerializerMethodField is not a custom field in that sense.
Use this, which is also doing the same as @extend_schema_field, but you have access to Union with that. It would also be possible to do this with @extend_schema_field but you would have to write a small raw schema.
Hello,
with my poor ability to find references on google, I finally ended up choosing this area for asking problems.
Assuming that I set a custom field using SerializerMethodfield that returns 2 different values by a certain condition as below.
It should return string or empty dict by a condition but All I can set for this was string type with only one value.
Is it technically wrong for the swagger to show different values or I can do more customizing with this?
The text was updated successfully, but these errors were encountered: