Home > Software engineering >  Data does not get saved in database ; unexpected keyword arguments
Data does not get saved in database ; unexpected keyword arguments

Time:01-25

I have gone through many solutions posted on SO and other places but have been running into the same issue. Pretty new to django and trying to understand where I am going wrong

I am getting the following error

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

I have the following JSON Data

    jsonToUse = {
        "CompanyId": "320193",
        "CashCashEquivalentsRestrictedCashAndRestrictedCashEquivalents": [
            {
                "decimals": "-6",
                "unitRef": "usd",
                "value": "39789000000"
            },
            {
                "decimals": "-6",
                "unitRef": "usd",
                "value": "50224000000"
            },
            {
                "decimals": "-6",
                "unitRef": "usd",
                "value": "25913000000"
            },
            {
                "decimals": "-6",
                "unitRef": "usd",
                "value": "35929000000"
            }
        ]
    }

Model:

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


class Cashcashequivalentsrestrictedcashandrestrictedcashequivalents(models.Model):
    cashcashequivalentsrestrictedcashandrestrictedcashequivalents_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)

Serializer:

class CashCashEquivalentsRestrictedCashAndRestrictedCashEquivalentsSerializer(serializers.ModelSerializer):

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


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

    class Meta:
        model = Basetable
        fields = "__all__"

View:

.....#TRIMMED GET SYNTAX.....
check = CashFlowSerializer(data=jsonToUse)
if (check.is_valid(raise_exception=True)):
    print("ready to send to db")
    check.save()
return JsonResponse(jsonToUse, safe=False)

I want to save the data in the database for the provided JSON

CodePudding user response:

Have a look to the full traceback error, the problem is that you need to define your own create method in your nested serializer:

TypeError: Got a `TypeError` when calling `Basetable.objects.create()`. 
This may be because you have a writable field on the serializer class that is not a valid argument to `Basetable.objects.create()`. 
You may need to make the field read-only, or override the CashFlowSerializer.create() method to handle this correctly.
...
TypeError: Basetable() got an unexpected keyword argument 'CashCashEquivalentsRestrictedCashAndRestrictedCashEquivalents'

You will find more information in django-rest-framework Writable nested serializers documentation.

Basically, iterate on your JSON data and create Cashequivalents objects.

  • Related