Home > Software engineering >  django rest_framework how to display nested relationship
django rest_framework how to display nested relationship

Time:12-06

I'm trying to display foreign related fields like this example and it works

    {
    "reqid": 10,
    "reqdate": "2022-12-05",
    "reqdescription": "Aircon Not working",
    "officerequestor": "OVCAA   ",
    "officeid": "PPD      ",
    "inspection": {
        "insdate": "2022-12-06",
        "diagnosis": "need to buy prism",
        "inspector": "EMP-322 "
    }
},

this is my serializers.py

class RequestAdditionDetailsSerializer(serializers.ModelSerializer):
class Meta:
    model = Inspection
    fields = ['insdate',
            'diagnosis',
            'inspector'
            ]

class RequestorSerializer(serializers.ModelSerializer):
inspection = RequestAdditionDetailsSerializer(read_only=True)
class Meta:
    model = Request
    fields = ['reqid',
            'reqdate',
            'reqdescription',
            'officerequestor',
            'officeid',
            'inspection'
            ]

My question is can I do this the other way around like this

    {
"inspectid": 5,
"reqid": "10",
"insdate": "2022-12-06",
"diagnosis": "need to buy prism",
"inspector": "EMP-322",
"isinspected": {
    "reqdescription": "Aircon Not working",
    "reqdate": "2022-12-05",
    "officerequestor": "OVCAA"
   } 
},

this is what I've tried, tbh I don't think this will work is there a solution for this.

if no maybe i'll add additional columns on inspection like reqdescription,reqdate etc.. just to show them

class InspectionAdditionalDetailsViewSerializer(serializers.ModelSerializer):
class Meta:
    model = Request
    fields = ['reqdescription',
            'reqdate',
            'officerequestor'
            ]

class InspectionSerializer(serializers.ModelSerializer):
request_details = InspectionAdditionalDetailsViewSerializer(read_only=True)
class Meta:
    model = Inspection
    fields = ['inspectid',
            'reqid',
            'insdate',
            'diagnosis',
            'inspector',
            'isinspected',
            'request_details'
            ]

this is my models.py

class Inspection(models.Model):
inspectid = models.AutoField(primary_key=True)
reqid = models.OneToOneField('Request', models.DO_NOTHING, db_column='reqid', blank=True, null=True)

class Meta:
    managed = False
    db_table = 'inspection'

class Request(models.Model):
reqid = models.AutoField(primary_key=True)

class Meta:
    managed = False
    db_table = 'request'

CodePudding user response:

You have defined the OneToOne field name reqid therefore you should use it as serializer key.

Noted that Django will add _id to the field so it will become reqid_id in your database, it's best to name it req or request only to refer to related object.

class InspectionAdditionalDetailsViewSerializer(serializers.ModelSerializer):
    class Meta:
        model = Request
        fields = [
            'reqdescription',
            'reqdate',
            'officerequestor',
        ]

class InspectionSerializer(serializers.ModelSerializer):
    reqid = InspectionAdditionalDetailsViewSerializer(read_only=True)
    class Meta:
        model = Inspection
        fields = [
            'inspectid',
            'reqid',
            'insdate',
            'diagnosis',
            'inspector',
            'isinspected',
        ]
  • Related