Serializing two CharFields into one object

Question:

I have serializer like this below. Now I have separated fields for store_photo and store_name.

class ProductSerializer(serializers.ModelSerializer):
    store_photo = serializers.CharField(source='store.photo', read_only=True)
    store_name = serializers.CharField(source='store.name', read_only=True)

    class Meta:
        model = Product
        fields = ['store_photo', 'store_name', ...]

Can I somehow serialize it together in one object? I mean something like this:

store = {store_photo, store_name}

class Meta:
        model = Product
        fields = ['store', ...]

Answers:

You can try SerializerMethodField

class ProductSerializer(serializers.ModelSerializer):
    calculated_store = serializers.SerializerMethodField()

    class Meta:
        model = Product
        fields = ['calculated_store', ...]

    def get_calculated_store(self, obj):
        return {obj.store.photo, obj.store.name}
Answered By: weAreStarDust