简体   繁体   中英

saving related nested object in Django Rest Framework

I am trying to post a message to the database using the following content on the request:

{
    "thread": 1,
    "content": "lorem ipsum",
    "author": 
    {
        "name":"doc",
        "email":""
    }

}

Instead I get the error message of "Cannot assign "OrderedDict([(u'name', doc'), (u'email', u'')])": "Message.author" must be a "Author" instance."

Each Message has a related author record, that may or may not exist already in the database. At this point I don't mind having repeat entries on that table.

My serializers.py file has the following:

class AuthorSerializer(serializers.Serializer):
    pk = serializers.IntegerField(read_only=False, required=False)
    name = serializers.CharField(required=True, max_length=50)
    email = serializers.CharField(allow_blank=True, allow_null=True, required=False)

    def create(self, validated_data):
        """
        Create and return a new `Author` instance, given the validated data.
        """
        return Author.objects.create(**validated_data)

    def update(self, instance, validated_data):
        """
        Update and return an existing `Author` instance, given the validated data.
        """
        instance.name = validated_data.get('name', instance.name)
        instance.email = validated_data.get('email', instance.email)
        instance.save()
        return instance

class Meta:
    model = Author
    fields = ('name', 'email')

class MessageSerializer(serializers.Serializer):
    pk = serializers.IntegerField(read_only=True)
    thread = serializers.PrimaryKeyRelatedField(queryset=Thread.objects.all())
    created_at = serializers.DateTimeField(required=False)
    content = serializers.CharField(style={'base_template': 'textarea.html'})
    author = AuthorSerializer(required=False, read_only=False)

    def create(self, validated_data):
        """
        Create and return a new `Message` instance, given the validated data.
        """
        return Message.objects.create(**validated_data)

    def update(self, instance, validated_data):
        """
        Update and return an existing `Message` instance, given the validated data.
        """
        instance.thread = validated_data.get('thread', instance.thread)
        instance.content = validated_data.get('content', instance.content)
        instance.author = validated_data.get('author', instance.author)
        instance.save()
        return instance

    class Meta:
        model = Message
        fields = ('thread', 'created_at', 'content', 'author')

I'm quite lost on how I should proceed, has I expected that AuthorSerializer would be able to parse a list or ordered dictionary into a new or existing object.

According to the docs , you have to create the nested object in your custom create method:

class MessageSerializer(serializers.Serializer):
    pk = serializers.IntegerField(read_only=True)
    thread = serializers.PrimaryKeyRelatedField(queryset=Thread.objects.all())
    created_at = serializers.DateTimeField(required=False)
    content = serializers.CharField(style={'base_template': 'textarea.html'})
    author = AuthorSerializer(required=False, read_only=False)

    def create(self, validated_data):
        """
        Create and return a new `Message` instance, given the validated data.
        """
        author_data = validated_data.pop('author', None)
        if author_data:
            author = Author.objects.get_or_create(**author_data)[0]
            validated_data['author'] = author
        return Message.objects.create(**validated_data)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM