Reputation: 1991
Have searched many answers but could not find what I am looking for:
I have two fields (one is foreign key) that would return something from the serializer that looks like this:
[{"prodID":"SV1", "Amount":"10"},
{"prodID":"RV1", "Amount":"37"},
{"prodID":"GG2", "Amount":"22"}]
What I would really want, (then I don't have to change my front-end) is:
[{"SV1":"10"},
{"RV1":"37"},
{"GG2":"22"}]
Would this be possible to do with django?
Upvotes: 0
Views: 62
Reputation: 47364
You can override serializer's to_representation
:
class SomeSerializer(serializers.ModelSerializer):
class Meta:
model = Some
def to_representation(self, instance):
result = super(SomeSerializer, self).to_representation(instance)
new_result = {result['prodID']: result['Amount']}
return new_result
Upvotes: 1