[英]How do I apply CodecOptions to a mongoengine collection to get tzinfo?
Using a mongoengine Document
, when I load a DateTimeField
from mongo, it lacks tzinfo
.使用 mongoengine Document
,当我从 mongo 加载DateTimeField
时,它缺少tzinfo
。 Our application by policy wants all datetimes to have tzinfo
.我们的策略应用程序希望所有日期时间都有tzinfo
。
When I save to mongo, I know that pymongo does the correct and predictable thing wrt timezones;当我保存到 mongo 时,我知道 pymongo 会在时区上做正确且可预测的事情; if it is naive, it is stored as a UTC time, if it has a timezone, it is converted to UTC and then stored as UTC time.如果它是天真的,则存储为 UTC 时间,如果它有时区,则将其转换为 UTC,然后存储为 UTC 时间。 All good so far.到目前为止一切都很好。
When I load, though, DateTimeField
always gives me a naive datetime
.但是,当我加载时, DateTimeField
总是给我一个天真的datetime
。 I know that this datetime
is in UTC, so I could add the tzinfo
if I wanted, but I'd have to do this in dozens of places in my application, and it's a guarantee of future timezone bugs, unless I use a custom field type (see example below).我知道这个datetime
是 UTC,所以我可以根据需要添加tzinfo
,但我必须在我的应用程序的几十个地方这样做,这是未来时区错误的保证,除非我使用自定义字段类型(见下面的例子)。
Referencing: https://api.mongodb.com/python/current/examples/datetimes.html I know that pymongo supports putting the tzinfo
onto the object as it arrives from the database.参考: https://api.mongodb.com/python/current/examples/datetimes.html我知道 pymongo 支持将tzinfo
从数据库到达时放到对象上。 I also know that I can do this myself, as the following minimal example shows.我也知道我自己可以做到这一点,如下面的最小示例所示。 The DateTimeTZField
adds tzinfo
in to_python
. DateTimeTZField
在to_python
添加了tzinfo
。
from datetime import datetime
from mongoengine import connect, Document, fields
from pytz import timezone
def utcnowTZ():
return datetime.utcnow().replace(tzinfo=timezone('UTF'))
class DateTimeTZField(fields.DateTimeField):
"""
This seems like a hack. I would like to use CodecOptions instead
"""
def to_python(self, value):
converted = super(DateTimeTZField, self).to_python(value)
return converted.replace(tzinfo=timezone('UTC'))
class Thing(Document):
dtTZ = DateTimeTZField(default=utcnowTZ)
dtXX = fields.DateTimeField(default=utcnowTZ)
connect(host="mongodb://localhost/datetimewithtz")
Thing.objects().delete()
t1 = Thing()
print '%r.dtXX (default): %s' % (t1, t1.dtXX)
print '%r.dtTZ (default): %s' % (t1, t1.dtTZ)
t1.save()
print 'saved %r' % t1.id; print
t1 = Thing.objects(id=t1.id).first()
print 'reloaded %r' % t1.id
print '%r.dtXX (loaded ): %s' % (t1, t1.dtXX)
print '%r.dtTZ (loaded ): %s' % (t1, t1.dtTZ)
Without replacing DateTimeField throughout my application, is there a way to use bson CodecOptions to make this apply globally to all DateTimeFields: when loaded from the database, they should have tzinfo attached ?在不替换整个应用程序中的 DateTimeField 的情况下,有没有办法使用 bson CodecOptions使其全局应用于所有 DateTimeFields:从数据库加载时,它们应该附加 tzinfo ?
How about怎么样
connect(..., tz_aware=True,...)
the parameter will be forwarded to MongoClient(..., tz_aware=True, ...)
参数将被转发到MongoClient(..., tz_aware=True, ...)
My understanding is that mongoengine doesn't support per-model codec_options
.我的理解是 mongoengine 不支持每个模型的codec_options
。 If it did, they should be part of the opts
dictionary here .如果是这样,它们应该是这里opts
字典的一部分。
However, PyMongo defaults to the Database's codec_options
when the Collection doesn't have any of it's own, and you can set these by manually calling register_connection
in your application before you make any requests to the DB.但是,当 Collection 没有自己的任何codec_options
时,PyMongo 默认为数据库的codec_options
,并且您可以在向数据库发出任何请求之前通过在应用程序中手动调用register_connection
来设置这些。
In your example above you should pass the codec_options
like this:在上面的示例中,您应该像这样传递codec_options
:
with_timezone = CodecOptions()
connect(host="mongodb://localhost/datetimewithtz", codec_options=with_timezone)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.