class Record(ndb.Model):
notes = ndb.TextProperty()
last_updated = ndb.DateTimeProperty(auto_now=True)
部分单元测试设置:
record2 = Record()
# trying to set the last_updated timestamp to a previous date
record2.last_updated = previous_date
record2.put()
#after saving it, the timestamp is back to today's date
因此我无法模拟单元测试的旧记录。如何在不更改模型的情况下覆盖该字段?
最佳答案
来自 docs
It is possible to override the value for a property with
auto_now_add=True
, but not for one withauto_now=True
. The automatic value is not generated until the entity is written; that is, these options don't provide dynamic defaults. (These details differ from the old db API.)
关于python - 如何覆盖时间戳字段以进行单元测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19379133/