diff --git a/AUTHORS b/AUTHORS index ad070b30..758c52ea 100644 --- a/AUTHORS +++ b/AUTHORS @@ -235,3 +235,4 @@ that much better: * Steven Rossiter (https://github.com/BeardedSteve) * Luo Peng (https://github.com/RussellLuo) * Bryan Bennett (https://github.com/bbenne10) + * Gilb's Gilb's (https://github.com/gilbsgilbs) diff --git a/mongoengine/fields.py b/mongoengine/fields.py index 6f69465c..35d5f35a 100644 --- a/mongoengine/fields.py +++ b/mongoengine/fields.py @@ -19,7 +19,7 @@ else: import pymongo import gridfs -from bson import Binary, DBRef, SON, ObjectId +from bson import Binary, DBRef, SON, ObjectId, Int64 from mongoengine.errors import ValidationError from mongoengine.python_support import (PY3, bin_type, txt_type, @@ -227,6 +227,9 @@ class LongField(BaseField): pass return value + def to_mongo(self, value, **kwargs): + return Int64(value) + def validate(self, value): try: value = long(value) diff --git a/tests/fields/fields.py b/tests/fields/fields.py index 1c1f49ad..2695e849 100644 --- a/tests/fields/fields.py +++ b/tests/fields/fields.py @@ -21,6 +21,7 @@ except ImportError: from decimal import Decimal from bson import Binary, DBRef, ObjectId +from bson.int64 import Int64 from mongoengine import * from mongoengine.connection import get_db @@ -4114,6 +4115,19 @@ class EmbeddedDocumentListFieldTestCase(unittest.TestCase): self.assertTrue(hasattr(CustomData.c_field, 'custom_data')) self.assertEqual(custom_data['a'], CustomData.c_field.custom_data['a']) + def test_long_field_is_stored_as_long(self): + """ + Tests that long fields are stored as long in mongo, even if long value + is small enough to be an int. + """ + class TestDocument(Document): + some_long = LongField() + + doc = TestDocument(some_long=42).save() + db = get_db() + self.assertTrue(isinstance(get_db().test_document.find()[0]['some_long'], Int64)) + self.assertTrue(isinstance(doc.some_long, (int, long,))) + if __name__ == '__main__': unittest.main()