Arrumar e organizar models e serializers. Rescrever método create de EventSerializer

parent 40ed287e
from eventos.core.models.event import Event
from eventos.core.models.event_ref import EventRef
from eventos.core.models.submission import Submission
from eventos.core.models.user import User
from eventos.core.models.embedded import EventSubmission, Owner
from eventos.core.models.user_ref import UserRef
from mongoengine import Document, fields
from eventos.core.models import UserRef
class Event(Document):
......@@ -9,3 +11,4 @@ class Event(Document):
ends_on = fields.DateTimeField(required=True)
local = fields.StringField(max_length=255, required=True)
description = fields.StringField(max_length=255, required=True)
owners = fields.EmbeddedDocumentListField(UserRef, required=False)
from mongoengine import EmbeddedDocument, fields
from eventos.core.models import Event
class EventRef(EmbeddedDocument):
id = fields.ReferenceField(Event, required=True)
name = fields.StringField(max_length=255, required=True)
name_english = fields.StringField(max_length=255, required=True)
from mongoengine import Document, fields
from eventos.core.models import EventRef
class Submission(Document):
......@@ -7,3 +9,4 @@ class Submission(Document):
type = fields.StringField(max_length=255, required=True)
info = fields.StringField(max_length=255, required=True)
status = fields.BooleanField(default=False)
event_ref = fields.EmbeddedDocumentField(EventRef, required=True)
from mongoengine import EmbeddedDocument, fields
from eventos.core.models import Event, User
from eventos.core.models import User
class Owner(EmbeddedDocument):
class UserRef(EmbeddedDocument):
id = fields.ReferenceField(User, required=True)
first_name = fields.StringField(max_length=255, required=True)
last_name = fields.StringField(max_length=255, required=True)
email = fields.EmailField(max_length=255, required=True)
class EventSubmission(EmbeddedDocument):
id = fields.ReferenceField(Event, required=True)
name = fields.StringField(max_length=255, required=True)
name_english = fields.StringField(max_length=255, required=True)
from eventos.core.serializers.embedded import EventSubmissionSerializer, OwnerSerializer
from eventos.core.serializers.embedded import EventRefSerializer, UserRefSerializer
from eventos.core.serializers.event import EventSerializer
from eventos.core.serializers.submission import SubmissionSerializer
from eventos.core.serializers.user import UserSerializer
from rest_framework_mongoengine import serializers
from eventos.core.models import EventSubmission, Owner
from eventos.core.models import EventRef, UserRef
class OwnerSerializer(serializers.EmbeddedDocumentSerializer):
class UserRefSerializer(serializers.EmbeddedDocumentSerializer):
class Meta:
model = Owner
model = UserRef
fields = "__all__"
class EventSubmissionSerializer(serializers.EmbeddedDocumentSerializer):
class EventRefSerializer(serializers.EmbeddedDocumentSerializer):
class Meta:
model = EventSubmission
model = EventRef
from rest_framework_mongoengine import serializers
from eventos.core.models import Event
from eventos.core.serializers import OwnerSerializer
from eventos.core.models import Event, UserRef
from eventos.core.serializers import UserRefSerializer
class EventSerializer(serializers.DocumentSerializer):
owners = OwnerSerializer(many=True)
owners = UserRefSerializer(many=True)
class Meta:
ref_name = "Event"
model = Event
fields = ["id", "name", "name_english", "starts_on", "ends_on", "local", "description", "owners"]
fields = "__all__"
read_only_fields = ("id",)
def create(self, validated_data):
owners_data = validated_data.pop("owners")
event = Event.objects.create(**validated_data)
event.owners = []
for owner_data in owners_data:
event.owners.append(UserRef(**owner_data))
event.save()
return event
# def update(self, instance, validated_data):
# owners_data = validated_data.pop("owners")
# updated_instance = super(EventSerializer, self).update(instance, validated_data)
# for owner_data in owners_data:
# updated_instance.owner_data.append(UserRef(**owner_data))
# updated_instance.save()
# return updated_instance
from rest_framework_mongoengine import serializers
from eventos.core.models import Submission
from eventos.core.serializers import EventSubmissionSerializer, OwnerSerializer
from eventos.core.serializers import EventRefSerializer, UserRefSerializer
class SubmissionSerializer(serializers.DocumentSerializer):
eventSubmission = EventSubmissionSerializer(many=False)
owners = OwnerSerializer(many=True)
event_ref = EventRefSerializer(many=False)
owners = UserRefSerializer(many=True)
class Meta:
ref_name = "Submission"
model = Submission
fields = ["id", "title", "type", "info", "status", "eventSubmission", "owners"]
fields = ["id", "title", "type", "info", "status", "event_ref", "owners"]
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