[英]Django rest framework: Nested object not passing to validated_data
This is surely a problem in my understanding of how the serializer should work. 在我对串行器应如何工作的理解中,这肯定是一个问题。
After changing a property permissions on my serializer, I found out that my Author nested object is turning out empty on the validated_data. 更改序列化程序的属性权限后,我发现我的Author嵌套对象在validated_data上变成空的。
Here's my code : 这是我的代码:
class ThreadSerializer(serializers.Serializer):
class Meta:
model = Thread
queryset=Thread.objects.all()
fields = ('id', 'title', 'description', 'author', 'created_at')
pk = serializers.IntegerField(read_only=True)
title = serializers.CharField(required=False, allow_blank=False, max_length=100)
description = serializers.CharField(style={'base_template': 'textarea.html'}, required=False)
author = AuthorSerializer()
created_at = serializers.DateTimeField(required=False)
def create(self, validated_data):
"""
Create and return a new `Thread` instance, given the validated data.
"""
author_data = validated_data.pop('author')
if author_data:
author = Author.objects.get_or_create(**author_data)
validated_data['author'] = author
return Thread.objects.create(**validated_data)
The payload is also quite simple: { "title": "2", "description": "testing nested objects", "author": { "name": "ron", "email" : "email@mail.com" }}
有效负载也非常简单:
{ "title": "2", "description": "testing nested objects", "author": { "name": "ron", "email" : "email@mail.com" }}
Yet, on the validated_data variable all I see is an empty OrderedDict. 但是,在validated_data变量上,我看到的只是一个空的OrderedDict。
Can someone point me to where I should be fixing this? 有人可以指出我应该解决的地方吗?
The problem here was on the client-side. 这里的问题是在客户端。
Information was being passed as form-data and not as application/json on the ajax request. 信息在ajax请求中作为表单数据而不是作为application / json传递。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.