If I serialize a object using to_json with timezone unaware dates and then load them back with from_json, the datetime object is then timezone aware.
class TestDoc(Document):
d = DateTimeField()
a = TestDoc()
a.d = datetime.now()
print a.d # 2015-03-29 11:12:53.963929
a = TestDoc.from_json(a.to_json())
print a.d # 2015-03-29 11:12:53.963000+00:00
Care to create a pull request with a failing test case?
I'll try to provide a fix for it.
ping @vinczente
The issue is in bson package
take a look here
>>> import bson
>>> from bson import json_util
>>> from datetime import datetime
>>> k = datetime.now()
>>> k
datetime.datetime(2017, 7, 12, 20, 7, 44, 694669)
>>> json_util.dumps(k)
'{"$date": 1499890064694}'
>>> json_util.loads(json_util.dumps(k))
datetime.datetime(2017, 7, 12, 20, 7, 44, 694000, tzinfo=<bson.tz_util.FixedOffset object at 0x237d750>)
>>> j = json_util.loads(json_util.dumps(k))
>>> k ==j
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can't compare offset-naive and offset-aware datetimes
>>>
(mongoengine)dtdev@dtdev-centos mongoengine (master) $ pip list | grep bson
bson 0.4.8
Could you open a corresponding issue in https://github.com/py-bson/bson?
raised here. https://github.com/py-bson/bson/issues/64
Till then do you accept a temporary fix to MongoEngine?
I think it would be best to not pollute MongoEngine with temporary fixes related to other libraries. In your own code you can just .replace(tzinfo=None) for any datetime you need to compare.
I agree.
Most helpful comment
Care to create a pull request with a failing test case?
I'll try to provide a fix for it.