I am trying build a REST API using django. here is my models.py and serializers.py.
models.py
from django.db import models
class Person(models.Model):
city = models.CharField(max_length=100)
dob = models.DateField(editable=True)
personName = models.CharField(max_length=100)
class Meta:
ordering = ['-dob']
serailizers.py
from rest_framework import serializers
from .models import Person
class PersonSerializer(serializers.ModelSerializer):
class Meta:
model = Person
fields = [ 'id', 'city', 'dob', 'personName']
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.
[{
"id": 1,
"city": "Sidney",
"personName": "Giles",
"dob": "2011-02-02"
},
{
"id": 5,
"city": "Paulba",
"personName": "Sidney",
"dob": "2016-07-16"
}]
But i want the json in below shown format -
[
{
"id": 123,
"city": "Sidney",
"personInCity": [
{
"id": 1,
"city": "Sidney",
"personName": "Giles",
"dob": "2011-02-02"
},
{
"id": 5,
"city": "Paulba",
"personName": "Sidney",
"dob": "2016-07-16"
}
]
}
]
i am not getting what change needs to be done in Serializers.py