简体   繁体   English

如何在eve中设置默认的datetime值?

[英]How can I set a default datetime value in eve?

For example, for a key: 例如,对于一个键:

'joinDate' : {'type' : 'datetime'}

How can I make it automatically get posted with the current date without being explicitly given in POST method? 如何使它自动与当前日期一起发布,而无需在POST方法中明确给出?

You can define an Insert Event , like this: 您可以定义一个插入事件 ,如下所示:

from datetime import datetime

"""some code omitted"""

def insert_join_date(items):
    for item in items:
        item["joinDate"] = datetime.utcnow()

app.on_insert_your_schema += insert_join_date

The previous example is based on on_insert_<resource_name>(items) and applies to a specific resource -- your_schema in this case. 上一个示例基于on_insert_<resource_name>(items)并且适用于特定资源-在这种情况下为your_schema If this logic should by applied to all the schemas, then on_insert(resource_name, items) can be used. 如果此逻辑应应用到所有模式,则可以使用on_insert(resource_name, items)

You can use the Python datetime.datetime.now() method like so: 您可以像这样使用Python datetime.datetime.now()方法:

>>> import datetime
>>> datetime.datetime.now()
datetime.datetime(2016, 6, 30, 14, 51, 25, 977000)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM