Category Archives: Django

Django Rest Framework – Change returned fields in serializers.ModelSerializer

Model Serializers has it's built in advantages where it's easy to implement and you are happy with the naming convention of the table fields. What if you are not and need to customize the field names.

Scenario 1.

A GET web service call returns the following json string:

but I want to return the following json:

Here's the code to do it in serializers.py:

And here's what's in the api.py

Hope this helps someone.

Thanks for reading.

Django to return formatted Json

To control the json data that gets returned to the view.

Define a method in model that looks something like that:

The method as_queuelist_report in model controls what the json looks like.

In your view.py you will have a method that looks like:

I find this to be a neat trick to control how a json looks like when you want to control what gets returned to the front end.