Writable nested serializers for multiple items for same base class

I have been following the guide to write nested serializer. Previously, I had only one item in my json object which was CashCashEquivalentsRestrictedCashAndRestrictedCashEquivalents and I am able to save the data properly in the databae. I want to add NetIncomeLoss the same way but I am running into the issue below. Can someone please point me where my mistake is? I am unable to find documentation/example or an answer online

TypeError: Basetable() got unexpected keyword arguments: 'NetIncomeLoss'

Serializer:

class PeriodSerializer(serializers.ModelSerializer):
    instant = serializers.CharField(required=False, max_length=255)
    startDate = serializers.CharField(required=False, max_length=255)
    endDate = serializers.CharField(required=False, max_length=255)

    class Meta:
        model = Period
        fields = "__all__"
        extra_kwargs = {'cashcashequivalentsrestrictedcashandrestrictedcashequivalents_id': {
            'required': False}, 'netincomeloss_id': {
            'required': False}}


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

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


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

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


class CashFlowSerializer(serializers.ModelSerializer):
    CashCashEquivalentsRestrictedCashAndRestrictedCashEquivalents = CashCashEquivalentsRestrictedCashAndRestrictedCashEquivalentsSerializer(
        many=True)
    NetIncomeLoss = NetIncomeLossSerializer(
        many=True)

    class Meta:
        model = Basetable
        fields = "__all__"

    def create(self, validated_data):
        itemOneData = validated_data.pop(
            'CashCashEquivalentsRestrictedCashAndRestrictedCashEquivalents')
        cashflow = Basetable.objects.create(**validated_data)
        for data in itemOneData:
            period_data = data.pop("period")
            my_long_named_obj = Cashcashequivalentsrestrictedcashandrestrictedcashequivalents.objects.create(
                basetable_id=cashflow, **data)
            period_object = Period.objects.create(
                cashcashequivalentsrestrictedcashandrestrictedcashequivalents_id=my_long_named_obj, **period_data
            )

        itemTwoData = validated_data.pop(
            'NetIncomeLoss')
        cashflow = Basetable.objects.create(**validated_data)
        for data in itemTwoData:
            period_data = data.pop("period")
            my_long_named_obj = Netincomeloss.objects.create(
                basetable_id=cashflow, **data)
            period_object = Period.objects.create(
                netincomeloss_id=my_long_named_obj, **period_data
            )
        return cashflow

Model:

class Basetable(models.Model):
    basetable = models.AutoField(primary_key=True)
    CompanyId = models.IntegerField()


class Cashcashequivalentsrestrictedcashandrestrictedcashequivalents(models.Model):
    cashtable = models.AutoField(
        primary_key=True)
    unitRef = models.CharField(max_length=100)
    value = models.CharField(max_length=100)
    decimals = models.CharField(max_length=100)
    basetable_id = models.ForeignKey(Basetable, on_delete=models.CASCADE)


class Netincomeloss(models.Model):
    netincomeloss_id = models.AutoField(primary_key=True)
    unitRef = models.CharField(max_length=100)
    value = models.CharField(max_length=100)
    decimals = models.IntegerField()
    basetable_id = models.ForeignKey(Basetable, on_delete=models.CASCADE)


class Period(models.Model):
    period = models.AutoField(primary_key=True)
    instant = models.CharField(blank=False, max_length=100)
    startDate = models.CharField(blank=False, max_length=255)
    endDate = models.CharField(blank=False, max_length=255)
    cashcashequivalentsrestrictedcashandrestrictedcashequivalents_id = models.OneToOneField(
        Cashcashequivalentsrestrictedcashandrestrictedcashequivalents, on_delete=models.CASCADE)
    netincomeloss_id = models.OneToOneField(
        Netincomeloss, on_delete=models.CASCADE)

View:

def companyApiCall(request):
    if request.method == 'GET':
        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"
                }
            ]
        }
        check = CashFlowSerializer(data=jsonToUse)
        if (check.is_valid(raise_exception=True)):
            print("ready to send to db")
            check.save()
        return JsonResponse(jsonToUse, safe=False)

Thank you for the time and help

Back to Top