pkout
pkout

Reputation: 6726

EmbeddedDocument field never optional in Django Mongoengine REST framework

I am using Django REST framework with Mongoengine. When I attempt serialize an optional field on an embedded document, the framework still requires the field and returns the error message that the field cannot be left blank. How do I make fields optional on an EmbeddedDocument? It works fine for standard Document model objects, just not for EmbeddedDocument objects.

My model:

class Event(EmbeddedDocument):
    id = ObjectIdField(required=True, default=ObjectId())
    status = StringField(required=True, max_length=50)
    note = StringField(required=False, max_length=2000)
    created = DateTimeField(required=True, default=timezone.now())

My serializer:

class EventSerializer(EmbeddedDocumentSerializer):

    class Meta:
        model = Event
        depth = 2

    def validate(self, data):
        return data

Note that the field "note" is set to required=False. When I serialize the document, however, I still get an error message that the field can't be left blank. Thank you!

Upvotes: 4

Views: 423

Answers (1)

nico
nico

Reputation: 2121

I came across the same problem, I think you can mark the fields as blank=True and it should allow you to place nothing in those fields.

Upvotes: 0

Related Questions