简体   繁体   中英

Django - How to deserialize json into a nested object

I have the following problem. I need to create a complex object using data POSTed in json. In the models.py file, I have the main object called "Company":

class Company(models.Model):
    name = models.CharField(max_length=255)
    descr = models.CharField(max_length=255)

    def __str__(self):
        return self.name

Then I have "Building":

class Building(models.Model):
    name = models.CharField(max_length=255)
    city = models.CharField(max_length=255)
    location = models.TextField()
    company= models.ForeignKey("Company", related_name="buildings", on_delete=models.CASCADE)

And so on with other few classes (ie "Floor", "Area", "Room").

The json I get from the POST request is:

    {
    "id": 1,
    "buildings": [{
        "id": 3,
        "floors": [{
            "id": 1,
            "floor": -1,
            "descr": "underground",
            "building": 3
        }],
        "areas": [{
            "id": 1,
            "rooms": [{
                "id": 1,
                "floor": 1,
                "area": 1,
                "descr": "First Room"
            }],
            "descr": "Dev area",
            "building": {
                "id": 3,
                "name": "Sede",
                "city": "Rome",
                "location": "mylocation",
                "company": {
                    "id": 1,
                    "name": "Test",
                    "descr": "Test"
                }
            }
        }],
        "name": "Sede",
        "city": "Rome",
        "location": "mylocation",
        "company": 1
    }],
    "name": "Test",
    "descr": "Test"
}

When I try to create a "Company" object from the json I get, it only creates the following:

{
    "id": 1,
    "buildings": [],
    "name": "Test",
    "descr": "Test"
}

and not the full structure.

My serializers are the following:

class BuildingSerializer(serializers.ModelSerializer):
    floors= FloorSerializer(read_only=True, many=True)
    areas = AreaSerializer(read_only=True, many=True)
    class Meta:
        model = Building
        fields = '__all__'
        read_only_fields = ('id',)

class CompanySerializer(serializers.ModelSerializer):
    buildings= BuildingSerializer(read_only=True, many=True)
    class Meta:
        model = Company
        fields = '__all__'
        read_only_fields = ('id',)

How can I resolve this?

PS I know the json is a bit redundant!

99% of the times when you don't get the nested data is because you're sending a JSON payload with a form content type.

Make it explicit in your client that you're sending JSON by setting the Content-Type header to application/json and that should fix the content issue.

You specify buildings as a read-only field: http://www.django-rest-framework.org/api-guide/serializers/#specifying-read-only-fields

Try removing that parameter in the serializer:

class BuildingSerializer(serializers.ModelSerializer):
    floors= FloorSerializer(many=True)
    aree = AreaSerializer(many=True)
    class Meta:
        model = Building
        fields = '__all__'
        read_only_fields = ('id',)

class CompanySerializer(serializers.ModelSerializer):
    buildings= BuildingSerializer(many=True)
    class Meta:
        model = Company
        fields = '__all__'
        read_only_fields = ('id',)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM