1
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 - '可以用auto_now_add = True覆蓋屬性的值,但不能爲auto_now = True的屬性覆蓋值。直到寫入實體纔會生成自動值;也就是說,這些選項不提供動態默認值。 (這些細節與舊的db API不同。)'你可能不得不在設置測試的同時擺弄模型。 –
@蒂姆霍夫曼爲什麼不把它作爲回答而不是評論? – Lipis
我經常這樣做,當我不滿意它作爲答案:-) –