Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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 this issue Oct 3, 2021 · 2 comments

Comments

@Rowadz
Copy link

Rowadz commented Oct 3, 2021

Hi

I'm using

  • django-rest-framework-mongoengine==3.3.1
  • Django==2.0
  • python==3.8

And noticed a strange behavior:

  • in my mongo model definitions I added a new field with a default value
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
Copy link
Collaborator

BurkovBA commented Oct 3, 2021

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
Copy link
Author

Rowadz commented Oct 3, 2021

yes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants