Unable to return nested json

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

Your model doesn’t directly correspond to the nested structure you’re trying to create. My initial impression is that a ModelSerializer either isn’t going to work for this, or would be a lot more effort than it’s worth. (But that’s a guess - I’m far from knowing everything that can easily be done with a ModelSerializer.)
I think you may find it easer to create your desired structure directly and then serialize it. You can create a dict for just the city with a key of personInCity, then assign your output from PersonSerializer to it.