[Answered ]-How can I embed two fields together in serializer

2👍

You can use a nested serializer to make this work.

class FSerializer(serializers.Serializer):
    value = serializers.Field(source="f_value")
    metric = serializers.Field(source="f_metric")

class MyModelSerializer(serializers.ModelSerializer):
    somefield = serializers.Field()

    f = FSerializer(source="*")

This should give you the nested output you are looking for. You can find more information on nesting serializers in the Django REST Framework documentation.

Leave a comment