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.

 

Adhearsion Install error

Been trying to install adhearsion recently and encountered the following error:

ERROR: Error installing adhearsion:
ERROR: Failed to build gem native extension.

/Users/mynamehere/.rvm/rubies/ruby-1.9.3-p327/bin/ruby extconf.rb
checking for pcre.h... no
-----
PCRE is missing. You must install it as per the README @ https://github.com/benlangfeld/ruby_speech
-----
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers. Check the mkmf.log file for more
details. You may need configuration options.

It turns out you just need to go to ruby_speech plugin and follow the README and execute the following command:

brew install pcre