Use serializer in Django (GeoDjango) to include both model field and some related ForeignKey fields

19 Views Asked by At

In my Django project, I have two model Home and Tenant. model Home is based on geodjango model and has a PointField. Model Tenant has the person information, and also has a ForeignKey to a Home.

from django.db import models
from django.contrib.gis.db import models as geomodels

class Home(geomodels.Model):
    location = models.PointField()
    code = models.IntegerField()
    #  many other fields

class Tenant(models.Model):
    home = models.ForeignKey(Home, on_delete=models.CASCADE)
    person_name = models.TextField()
    #  many other fields

I use serializer to turn a Queryset of Homes into geojson. The thing is, I want to also include some fields of the last Tenant model object of each well in that geojson, too (As they are home fields, but with a prefix tenant__ to have seperated name from original home fields).

I also prefer not to use Django-Rest-Framework. Could you help me?

0

There are 0 best solutions below