Implement item creation.

This commit is contained in:
Tom Hacohen 2020-02-26 16:07:55 +02:00
parent 4054a2f78c
commit 452a8f1e7e

View File

@ -118,6 +118,20 @@ class CollectionItemSerializer(serializers.ModelSerializer):
model = models.CollectionItem model = models.CollectionItem
fields = ('uid', 'content') 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): def update(self, instance, validated_data):
"""Function that's called when this serializer is meant to update an item""" """Function that's called when this serializer is meant to update an item"""
revision_data = validated_data.pop('content') revision_data = validated_data.pop('content')