umutbozkurt/django-rest-framework-mongoengine — pull request #43 by mkhndrsn (2014-11-04 22:03:41+00:00)
Title: Allow ObjectIdField to be saved on from_native call

This fixes the following scenario where field 'other' is converted correctly, but 'object_id' is converted to None because field_from_native() always returns None in fields.Field. This should work if the native value is a string or ObjectId

import mongoengine
import rest_framework_mongoengine.serializers
from bson import ObjectId

class TestClass(mongoengine.Document):
object_id = mongoengine.ObjectIdField()
other = mongoengine.StringField()

class TestSerializer(rest_framework_mongoengine.serializers.MongoEngineModelSerializer):
class Meta:
model = TestClass
fields = ('id', 'object_id', 'other',)

test = TestSerializer(data={'object_id': ObjectId('516517387ca7077daa3f572d'), 'other': 'test'})
test.is_valid()
test.save()
print(test.object.object_id)
print(test.object.other)