django 数据未保存在数据库中;意外的关键字参数

dtcbnfnu  于 2023-01-27  发布在  Go
关注(0)|答案(1)|浏览(163)

我已经经历了许多解决方案张贴在SO和其他地方,但一直遇到同样的问题。相当新的django,并试图了解我哪里出错了
我收到以下错误
TypeError:Basetable()获得意外的关键字参数:'现金现金等价物限制性现金和限制性现金等价物'
我有以下JSON数据

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"
            }
        ]
    }

型号:

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)

串行器:

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__"

查看:

.....#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)

我想为提供的JSON保存数据库中的数据

9q78igpj

9q78igpj1#

查看完整的追溯错误,问题是您需要在嵌套的序列化程序中定义自己的create方法:

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'

你可以在django-rest-framework可写嵌套序列化器文档中找到更多信息。
基本上,迭代JSON数据并创建Cashequivalents对象。

相关问题