2013-10-15 36 views
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 

因此,我不能效仿我的單元測試的舊紀錄。如何覆蓋該字段而不必更改模型?

+0

來自docs - '可以用auto_now_add = True覆蓋屬性的值,但不能爲auto_now = True的屬性覆蓋值。直到寫入實體纔會生成自動值;也就是說,這些選項不提供動態默認值。 (這些細節與舊的db API不同。)'你可能不得不在設置測試的同時擺弄模型。 –

+0

@蒂姆霍夫曼爲什麼不把它作爲回答而不是評論? – Lipis

+0

我經常這樣做,當我不滿意它作爲答案:-) –

回答

2

docs

有可能與auto_now_add=True覆蓋一個屬性值,而不是一個與auto_now=True。直到寫入實體纔會生成自動值;也就是說,這些選項不提供動態默認值。 (這些細節與舊的db API不同)。