3

I have a json response from a web request which almost maps to my django model.

How do I serialize this json(preferably with a model serializer),but override one field, so I can map it to a differently named field on the Django model. (I have one field "expected_value" in the json object, but I want to map that to the "actual_value" of my Django model).

2
  • Did the answer work for you? Commented Nov 5, 2014 at 7:47
  • No, (sorry I can't remember what the problem was with that approach exactly). I ended up synchronizing my models, and using standard serialzers. Commented Nov 5, 2014 at 14:01

1 Answer 1

4

You can add extra fields to a ModelSerializer or override the default fields by declaring fields on the class, just as you would for a Serializer class.

Something like the code snippet below should work.

class MySerializer(serializers.ModelSerializer):
    expected = serializers.Field(source='actual')

    class Meta:
        model = MyModel
        fields = ('field1', 'field2', 'expected')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.