JSON [{"non_field_errors": ["Invalid data"]}]} Django Rest 框架
JSON [{"non_field_errors": ["Invalid data"]}]} Django Rest Framework
我有一些序列化程序,其中有一些元素并希望它在我为 InvoiceDictionarySerializer 创建 POST 方法时 POST 它。不知道如何使其正常工作。有什么建议吗?
我的代码如下所示:
class InvoiceDictionaryElementSerializer(serializers.ModelSerializer):
class Meta:
model = InvoiceDictionaryElement
fields = (
'name', 'unit', 'quantity', 'value'
)
class InvoiceDictionarySerializer(serializers.ModelSerializer):
invoice_elements = InvoiceDictionaryElementSerializer()
class Meta:
model = InvoiceDictionary
fields = (
'name', 'is_active', 'invoice_elements')
以及我对 JSON 的请求:
{
"name": "invoice",
"is_active": true,
"invoice_elements": [{
"name": "name",
"unit": "12",
"quantity": "15.00",
"value": "7.00"
}]
}
出现错误:
{"invoice_elements": [{"non_field_errors": ["Invalid data"]}]}
在我的 conslote 输出中出现 400 错误(错误请求)。不知道 JSON 的语法错误还是什么?
因为invoice_elements
是一个元素列表,你需要指定many=True
class InvoiceDictionarySerializer(serializers.ModelSerializer):
invoice_elements = InvoiceDictionaryElementSerializer(many=True)
class Meta:
model = InvoiceDictionary
fields = (
'name', 'is_active', 'invoice_elements')
def create(self, validated_data):
...
def update(self, instance, validated_data):
...
You should also implement .create()
或 .update()
处理保存多个对象的序列化程序的方法。
example_entry = serializers.CharField(required=True/False)
问题出在序列化程序字段中。用正确的标志配置它。
我有一些序列化程序,其中有一些元素并希望它在我为 InvoiceDictionarySerializer 创建 POST 方法时 POST 它。不知道如何使其正常工作。有什么建议吗?
我的代码如下所示:
class InvoiceDictionaryElementSerializer(serializers.ModelSerializer):
class Meta:
model = InvoiceDictionaryElement
fields = (
'name', 'unit', 'quantity', 'value'
)
class InvoiceDictionarySerializer(serializers.ModelSerializer):
invoice_elements = InvoiceDictionaryElementSerializer()
class Meta:
model = InvoiceDictionary
fields = (
'name', 'is_active', 'invoice_elements')
以及我对 JSON 的请求:
{
"name": "invoice",
"is_active": true,
"invoice_elements": [{
"name": "name",
"unit": "12",
"quantity": "15.00",
"value": "7.00"
}]
}
出现错误:
{"invoice_elements": [{"non_field_errors": ["Invalid data"]}]}
在我的 conslote 输出中出现 400 错误(错误请求)。不知道 JSON 的语法错误还是什么?
因为invoice_elements
是一个元素列表,你需要指定many=True
class InvoiceDictionarySerializer(serializers.ModelSerializer):
invoice_elements = InvoiceDictionaryElementSerializer(many=True)
class Meta:
model = InvoiceDictionary
fields = (
'name', 'is_active', 'invoice_elements')
def create(self, validated_data):
...
def update(self, instance, validated_data):
...
You should also implement .create()
或 .update()
处理保存多个对象的序列化程序的方法。
example_entry = serializers.CharField(required=True/False)
问题出在序列化程序字段中。用正确的标志配置它。