Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Rest Framework Multiple Nested Writable Serializers

Tags:

I am trying to implement multiple nested writable serializers using django rest framework. I have read the docs available http://www.django-rest-framework.org/api-guide/serializers/#writable-nested-representations

I have been able to do this for one level nests but have a problem when there are multiple nests. For example, I have these 3 serializers:

class FarmerSerializer(serializers.ModelSerializer):
    dob = serializers.DateField(write_only=True)
    gender = serializers.CharField(write_only=True)

    farms = FarmSerializer(required=False, many=True)

    class Meta:
        ...

    def create(self, validated_data):
        # check if nested objects were provided, in particular farms
        farms = validated_data.pop('farms', None)

        farmer = Farmer(**validated_data)
        farmer.added_by_id = validated_data.get('added_by_id')
        farmer.save()

        if farms is not None:
            for farm in farms:
                new_farm = Farm.objects.create(farmer=farmer, added_by=farmer.added_by, **farm)

                # check for blocks -- fails
                blocks = farm.pop('farm_blocks', None)
                if blocks is not None:
                    for block in blocks:
                        FarmBlock.objects.create(farm=new_farm, added_by=farmer.added_by, **block)

        return farmer

class FarmSerializer(serializers.ModelSerializer):
    county = CountySerializer(read_only=True)
    county_id = serializers.UUIDField(write_only=True)

    farm_blocks = FarmBlockSerializer(required=False, many=True)

class FarmBlockSerializer(serializers.ModelSerializer):
    ...

I'd like to be able to post JSON that is nested and save all 3 levels i.e farmer, farms and farm blocks. It currently works when I'm saving the first 2 levels i.e Farmer and associated farms. If it helps, here is the JSON structure I'm attempting to save:

[
    {
        "name": "Ashley King",
        "phone_number": "0765124764",
        "gender": "F",
        "dob": "1980-11-26",
        "national_id": "29719008",
        "farms": [
            {
                "name": "Big Farm 3",
                "farm_size": "18",
                "county_id": "5e208ba8-5f6c-4dac-a946-dada0c5250a2",
                "constituency": "Nakuru",
                "ward": "Town",
                "town": "Nakuru",
                "contact_name": "Winnie W.",
                "contact_phone_number": "0724301432",
                "farm_blocks": [
                    {"block_size": 4.56}
                ]
            },
            {
                "name": "Big Farm 4",
                "farm_size": "9.6",
                "county_id": "5e208ba8-5f6c-4dac-a946-dada0c5250a2",
                "constituency": "Nakuru",
                "ward": "Town",
                "town": "Nakuru",
                "contact_name": "Winnie W.",
                "contact_phone_number": "0724301432"
            }
        ]
    },
    {
        "name": "Dennis Wainaina",
        "phone_number": "0764578389",
        "gender": "M",
        "dob": "1988-11-26",
        "national_id": "27675654",
        "farms": [
            {
                "name": "Big Farm 6",
                "farm_size": "18",
                "county_id": "5e208ba8-5f6c-4dac-a946-dada0c5250a2",
                "constituency": "Nakuru",
                "ward": "Town",
                "town": "Nakuru",
                "contact_name": "Winnie W.",
                "contact_phone_number": "0724301432"
            },
            {
                "name": "Big Farm 5",
                "farm_size": "9.6",
                "county_id": "5e208ba8-5f6c-4dac-a946-dada0c5250a2",
                "constituency": "Nakuru",
                "ward": "Town",
                "town": "Nakuru",
                "contact_name": "Winnie W.",
                "contact_phone_number": "0724301432"
            }
        ]
    }
]
like image 338
Denny Avatar asked Jun 27 '17 07:06

Denny


1 Answers

Maybe you could edit your create method, somewhat like this,

def create(self, validated_data):
    farms = validated_data.pop('farms', None)
    farmer = Farmer.objects.create(**validated_data)

    if farms is not None:
        for farm in farms:
            #pop farm_blocks before creating new_farm
            blocks = farm.pop('farm_blocks', None)
            new_farm = Farm.objects.create(farmer=farmer, added_by=farmer.added_by, **farm)
            #then create farm_bocks
            if blocks is not None:
                for block in blocks:
                    FarmBlock.objects.create(farm=new_farm, added_by=farmer.added_by, **block)               

    return farmer
like image 163
zaidfazil Avatar answered Sep 21 '22 23:09

zaidfazil