Reputation: 53
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.
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 protected]" }}
Yet, on the validated_data variable all I see is an empty OrderedDict.
Can someone point me to where I should be fixing this?
Upvotes: 2
Views: 1366
Reputation: 53
The problem here was on the client-side.
Information was being passed as form-data and not as application/json on the ajax request.
Upvotes: 1