umutbozkurt / django-rest-framework-mongoengine

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

DynamicDocumentSerializer doesn't include embedded field #175

Open gauravvjn opened 8 years ago

gauravvjn commented 8 years ago
class MyModel2(mongoengine.EmbeddedDocument):
     sn = mongoengine.StringField()
     # more fields

class MyModel(mongoengine.DynamicDocument):
    field1 = mongoengine.StringField(unique=True)
    field2 = mongoengine.DateTimeField()
    field3 = mongoengine.BooleanField()
    field4 = mongoengine.EmbeddedDocumentField(MyModel2)

and here is the serializer

from rest_framework_mongoengine.serializers import DynamicDocumentSerializer

class MyModelSerializer(DynamicDocumentSerializer):
    class Meta:
        model = MyModel
        fields = ('field1', 'field4')

and view

class myListAPI(generics.ListAPIView):
     model = MyModel
     serializer_class = MyModelSerializer
     queryset = MyModel.objects.all()

but this api returns only field1, field2, field3. even after explicitly mentioning the field4.

PS: I'm using django-rest-framework-mongoengine==3.3.0 mongoengine==0.10.5

BurkovBA commented 8 years ago

I believe, you need to explicitly define MyModel2Serializer and declare it as a field on MyModelSerializer:

class MyModel2Serializer(EmbeddedDocumentSerializer):
    class Meta:
        model = MyModel2

class MyModelSerializer(DynamicDocumentSerializer):
    field4 = MyModel2Serializer()

    class Meta:
        model = MyModel
        fields = ('field1', 'field4')

I've written a unit-test for your case and it is partially successful (though, fails for create(), which I should sort out).

BurkovBA commented 8 years ago

@gjain0 Could you try pulling the most recent commit and testing your case with it? I think it should be working by now. At least, similar unit-tests pass.