DRF: What is 'source' in Fields?
1 min read

DRF: What is 'source' in Fields?

DRF: What is 'source' in Fields?

For a couple of days I've been trying to de-serialise a JSON to a model. My problem is the model and the JSON have different fields, mainly to keep the model pythonic and independent of a specific JSON source. So...

I've been trying to use the source parameter for each Field in my serialiser to map to the JSON. E.g. if my Django Model field would be named timestamp and my JSON would be named time, I would have a serialiser Field like this:

timestamp = TimestampField(source='time')


It's actually exactly the opposite:

  • the variable name is the JSON key
  • the source name is the Django Model's field name

So, logically, the correct way is:

time = TimestampField(source='timestamp')

I've only noticed this while debugging through DRF's serializers.py (look for OrderedDict references).