[Answered ]-Serialization of a value from two models with links to foreign key

1👍

If you want to get fields of modelC from modelA serializer then this code will works.

class ModelCserializer(serializer.ModelSerializer):
    class Meta:
        model = ModelC
        fields = ('some_field',)

class ModelASerializer(serializers.ModelSerializer):
    here_is_value_from_ModelC= ModelCserializer(read_only=True)
    class Meta:
        model = ModelA
        fields = ('id', 'some_field', 'here_is_value_from_ModelC')

or

class ModelASerializer(serializers.ModelSerializer):
    some_field= serializer.StringRelatedField(many=True)
    class Meta:
        model = ModelA
        fields = ('id', 'some_field', 'some_field')

Leave a comment