Adicionar métodos .create() e .update() ao SubmissionSerializer

parent e4004940
from rest_framework_mongoengine import serializers from rest_framework_mongoengine import serializers
from eventos.core.models import Submission from eventos.core.models import Submission, UserRef
from eventos.core.serializers import EventRefSerializer, UserRefSerializer from eventos.core.serializers import EventRefSerializer, UserRefSerializer
...@@ -11,4 +11,25 @@ class SubmissionSerializer(serializers.DocumentSerializer): ...@@ -11,4 +11,25 @@ class SubmissionSerializer(serializers.DocumentSerializer):
class Meta: class Meta:
ref_name = "Submission" ref_name = "Submission"
model = Submission model = Submission
fields = ["id", "title", "type", "info", "status", "event_ref", "owners"] fields = "__all__"
def create(self, validated_data):
owners_data = validated_data.pop("owners")
submission = Submission.objects.create(**validated_data)
submission.owners = []
for owner_data in owners_data:
submission.owners.append(UserRef(**owner_data))
submission.save()
return submission
def update(self, instance, validated_data):
owners_data = validated_data.pop("owners")
submission = super(SubmissionSerializer, self).update(instance, validated_data)
for owner_data in owners_data:
submission.owners.append(UserRef(**owner_data))
submission.save()
return submission
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment