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

EmbeddedDocument field in DocumentSerializer can't be set to null #256

Open
sebitoelcheater opened this issue Aug 4, 2018 · 3 comments
Open

Comments

@sebitoelcheater
Copy link

I have a Charger document that contains a discount EmbeddedDocumentField. I also have a serializer for Charger.

class Discount(mongoengine.EmbeddedDocument):
    amount = mongoengine.FloatField(default=0)
    start = mongoengine.DateTimeField(default=datetime.datetime.fromtimestamp(0))
    end = mongoengine.DateTimeField(default=datetime.datetime.fromtimestamp(253400999999))

class Charger(Document):
    discount = mongoengine.EmbeddedDocumentField(Discount, null=True, required=False)

class ChargerSerializer(DocumentSerializer):
    class Meta:
        model = Charger
        fields = ('id', 'discount')
        extra_kwargs = {
            'discount': {'required': False, 'allow_null': True}
        }

When I call save on ChargerSerializer with discount set to None, it raises this error

  File "/Users/sebito/Developer/Proyectos/cargoo/cargoo/api/serializers/profile_serializer.py", line 89, in handle_update
    instanced_serializer.save()
  File "/Users/sebito/.virtualenvs/cargoo/lib/python3.6/site-packages/rest_framework/serializers.py", line 209, in save
    self.instance = self.update(self.instance, validated_data)
  File "/Users/sebito/.virtualenvs/cargoo/lib/python3.6/site-packages/rest_framework_mongoengine/serializers.py", line 269, in update
    instance = self.recursive_save(validated_data, instance)
  File "/Users/sebito/.virtualenvs/cargoo/lib/python3.6/site-packages/rest_framework_mongoengine/serializers.py", line 230, in recursive_save
    me_data[key] = field.recursive_save(value)
  File "/Users/sebito/.virtualenvs/cargoo/lib/python3.6/site-packages/rest_framework_mongoengine/serializers.py", line 224, in recursive_save
    for key, value in validated_data.items():
AttributeError: 'NoneType' object has no attribute 'items'

in recursive_save method of DocumentSerializer, it's assumed that validated_data won't be null but it might.

@BurkovBA
Copy link
Collaborator

BurkovBA commented Aug 4, 2018

Can you try going with the implicit EmbeddedDocumentSerializer for discount, without creating one of your own? Also, make sure that the versions of libraries are exactly as follows:

django==1.9
djangorestframework==3.3.3
mongoengine==0.9
pymongo==2.7

@sebitoelcheater
Copy link
Author

What do you think about this fix?
sebitoelcheater@16f7bef

@vijesh-venugopal
Copy link

vijesh-venugopal commented Apr 22, 2020

I'm also facing the same issue related to the EmbeddedDocumentSerializer and here the value for EmbeddedDocumentSerializer field is an object of model. It returns the error like above:

AttributeError: 'Modelname' object has no attribute 'items'
Any suggestions please.

Package versions:
django == 3.0.4
djangorestframework == 3.11.0
mongoengine == 0.19.1
pymongo == 3.10.1
django-rest-framework-mongoengine == 3.4.1

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

3 participants