DRF: What is ‘source’ in Fields?

Posted by in Software, Tutorial

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:

THIS IS WRONG!

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:

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

HTH,


A little experiment: If you find this post and ad below useful, please check the ad out :-)