我已经经历了许多解决方案张贴在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保存数据库中的数据
1条答案
按热度按时间9q78igpj1#
查看完整的追溯错误,问题是您需要在嵌套的序列化程序中定义自己的create方法:
你可以在django-rest-framework可写嵌套序列化器文档中找到更多信息。
基本上,迭代JSON数据并创建
Cashequivalents
对象。