Skip to content

Commit

Permalink
ObjectId serialisation on ListField(ReferenceField) fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Umut Bozkurt committed Sep 14, 2014
1 parent 203fe66 commit 6495878
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions rest_framework_mongoengine/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from mongoengine.base.document import BaseDocument
from mongoengine.document import Document
from rest_framework import serializers
from mongoengine.fields import ObjectId

import sys

if sys.version_info[0] >= 3:
Expand Down Expand Up @@ -66,8 +68,8 @@ def transform_object(self, obj, depth):
elif obj is None:
return None
else:
# Default to string
return unicode(obj)
print obj
return unicode(obj) if isinstance(obj, ObjectId) else obj


class ReferenceField(MongoDocumentField):
Expand Down Expand Up @@ -101,7 +103,7 @@ def from_native(self, value):
return self.model_field.to_python(value)

def to_native(self, obj):
return [self.model_field.to_mongo(list_item) for list_item in obj]
return self.transform_object(obj, self.depth)


class EmbeddedDocumentField(MongoDocumentField):
Expand Down Expand Up @@ -134,4 +136,4 @@ class DynamicField(MongoDocumentField):
type_label = 'DynamicField'

def to_native(self, obj):
return self.model_field.to_python(obj)
return self.model_field.to_python(obj)

0 comments on commit 6495878

Please sign in to comment.