Skip to content
Advertisement

how to return nested json using django rest framework

I am trying build a REST API using django. here is my models.py and serializers.py.

models.py

JavaScript

serailizers.py

JavaScript

Here is my api – http://127.0.0.1:8000/api/city/Sidney/. I am trying to fetch data by city name.

I am getting the json in below format.

JavaScript

But i want the json in below shown format –

JavaScript

i am not getting what change needs to be done in Serializers.py

Advertisement

Answer

To get correct response you might need to change your models.py and Serializers.py as below

JavaScript

Should give you the response like this:

Screenshot of API response, as per your question

Advertisement