umutbozkurt / django-rest-framework-mongoengine

Mongoengine support for Django Rest Framework
MIT License
616 stars 167 forks source link

The DocumentSerializer uses the default value from the model definition when the field does not even exists in the document yet #285

Open Rowadz opened 2 years ago

Rowadz commented 2 years ago

Hi

I'm using

And noticed a strange behavior:

class Component(Document):
    title = StringField(null=True)
    new_field = StringField(default="THE_DEFAULT_VALUE")

My serializer

from rest_framework_mongoengine.serializers import DocumentSerializer

class MySerializers(DocumentSerializer):
   class Meta:
      model = Component
      fields = ['id', 'title', 'new_field']

The response, in this case, will contain the filed new_field with the value THE_DEFAULT_VALUE in all of the returned objects even though this new_field is not in any of the documents yet

Is this is the default behavior or I'm missing something? because it makes much more sense that we don't return values that are not in the DB unless it was explicitly defined in the serializer to do that.

BurkovBA commented 2 years ago

Hello.

Do you mean that you created some Component documents in the database with new_field field missing, but the serializer still returns JSONs with "new_field = "THE DEFAULT VALUE"" as if it were there?

Rowadz commented 2 years ago

yes