Django Code Repetition due to Multiple objects in JSON

I have the following code which works as expected i.e saves data to the db as I would like it to, however, there is a lot of code repetition and I am unable to find a way to shorten the code

I have about 30 serializers (pasting 3 to shorten the code)

class FirstOne(serializers.ModelSerializer):
    period = PeriodSerializer(many=False)

    class Meta:
        model = FirstOne
        fields = ['decimals', 'unitRef', 'value', 'period']


class SecondOne(serializers.ModelSerializer):
    period = PeriodSerializer(many=False)

    class Meta:
        model = SecondOne
        fields = ['decimals', 'unitRef', 'value', 'period']


class ThirdOne(serializers.ModelSerializer):
    period = PeriodSerializer(many=False)

    class Meta:
        model = ThirdOne
        fields = ['decimals', 'unitRef', 'value', 'period'] 

class CashFlowSerializer(serializers.ModelSerializer):
    FirstItemInJson = FirstOne(many=True)
    SecondItemInJson = SecondOne(many=True)
    ThirdItemInJson = ThirdOne(many=True)

class Meta:
    model = Basetable
    fields = "__all__"

def create(self, validated_data):
    itemOneData = validated_data.pop('FirstItemInJson')
    itemTwoData = validated_data.pop('SecondItemInJson')
    itemThreeData = validated_data.pop('ThirdItemInJson')
cashflow = Basetable.objects.create(**validated_data)
for data in itemOneData:
    period_data = data.pop("period")
    dataObj = FirstItemModelClass.objects.create(
        basetable_id=cashflow, **data)
    period_object = Period.objects.create(
        firstItem_id=dataObj, **period_data)
for data in itemTwoData:
    period_data = data.pop("period")
    dataObj = SecondItemModelClass.objects.create(
        basetable_id=cashflow, **data)
    period_object = Period.objects.create(
        secondItem_id=dataObj, **period_data)
for data in itemThreeData:
    period_data = data.pop("period")
    dataObj = ThirdItemModelClass.objects.create(
        basetable_id=cashflow, **data)
    period_object = Period.objects.create(
        thirdItem_id=dataObj, **period_data)

JSON: (trimmed to keep it shorter)

jsonToUse = {
    "CompanyId": "320193",
    "CashCashEquivalentsRestrictedCashAndRestrictedCashEquivalents": [
        {
            "decimals": "-6",
            "unitRef": "usd",
            "period": {
                "instant": "2020-09-26"
            },
            "value": "39789000000"
        },
        {
            "decimals": "-6",
            "unitRef": "usd",
            "period": {
                "instant": "2019-09-28"
            },
            "value": "50224000000"
        },
        {
            "decimals": "-6",
            "unitRef": "usd",
            "period": {
                "instant": "2018-09-29"
            },
            "value": "25913000000"
        },
        {
            "decimals": "-6",
            "unitRef": "usd",
            "period": {
                "instant": "2021-09-25"
            },
            "value": "35929000000"
        }
    ],
    "NetIncomeLoss": [
        {
            "decimals": "-6",
            "unitRef": "usd",
            "period": {
                "startDate": "2020-09-27",
                "endDate": "2021-09-25"
            },
            "value": "94680000000"
        },
        {
            "decimals": "-6",
            "unitRef": "usd",
            "period": {
                "startDate": "2019-09-29",
                "endDate": "2020-09-26"
            },
            "value": "57411000000"
        },
        {
            "decimals": "-6",
            "unitRef": "usd",
            "period": {
                "startDate": "2018-09-30",
                "endDate": "2019-09-28"
            },
            "value": "55256000000"
        },
        {
            "decimals": "-6",
            "unitRef": "usd",
            "period": {
                "startDate": "2020-09-27",
                "endDate": "2021-09-25"
            },
            "segment": {
                "dimension": "us-gaap:StatementEquityComponentsAxis",
                "value": "us-gaap:RetainedEarningsMember"
            },
            "value": "94680000000"
        },
        {
            "decimals": "-6",
            "unitRef": "usd",
            "period": {
                "startDate": "2019-09-29",
                "endDate": "2020-09-26"
            },
            "segment": {
                "dimension": "us-gaap:StatementEquityComponentsAxis",
                "value": "us-gaap:RetainedEarningsMember"
            },
            "value": "57411000000"
        },
        {
            "decimals": "-6",
            "unitRef": "usd",
            "period": {
                "startDate": "2018-09-30",
                "endDate": "2019-09-28"
            },
            "segment": {
                "dimension": "us-gaap:StatementEquityComponentsAxis",
                "value": "us-gaap:RetainedEarningsMember"
            },
            "value": "55256000000"
        }
    ],
    "DepreciationDepletionAndAmortization": [
        {
            "decimals": "-6",
            "unitRef": "usd",
            "period": {
                "startDate": "2020-09-27",
                "endDate": "2021-09-25"
            },
            "value": "11284000000"
        },
        {
            "decimals": "-6",
            "unitRef": "usd",
            "period": {
                "startDate": "2019-09-29",
                "endDate": "2020-09-26"
            },
            "value": "11056000000"
        },
        {
            "decimals": "-6",
            "unitRef": "usd",
            "period": {
                "startDate": "2018-09-30",
                "endDate": "2019-09-28"
            },
            "value": "12547000000"
        }
    ]}

Given I have 30 items in my JSON, currently I am going through each item using a for loop so that I can store the Period data in that. How can I shorten my code?

Thank you for your time and help.

Back to Top