From 452a8f1e7effef4db934d0513b5529d46f44ec81 Mon Sep 17 00:00:00 2001 From: Tom Hacohen Date: Wed, 26 Feb 2020 16:07:55 +0200 Subject: [PATCH] Implement item creation. --- django_etesync/serializers.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/django_etesync/serializers.py b/django_etesync/serializers.py index b3f7254..a576b6d 100644 --- a/django_etesync/serializers.py +++ b/django_etesync/serializers.py @@ -118,6 +118,20 @@ class CollectionItemSerializer(serializers.ModelSerializer): model = models.CollectionItem fields = ('uid', 'content') + def create(self, validated_data): + """Function that's called when this serializer creates an item""" + revision_data = validated_data.pop('content') + instance = self.__class__.Meta.model(**validated_data) + + with transaction.atomic(): + instance.save() + + chunks = revision_data.pop('chunks') + revision = models.CollectionItemRevision.objects.create(**revision_data, item=instance) + revision.chunks.set(chunks) + + return instance + def update(self, instance, validated_data): """Function that's called when this serializer is meant to update an item""" revision_data = validated_data.pop('content')