1

我就遇到了這個功能(?)其中字典是隱式轉換爲ndb.Model對象隱快譯通在Python谷歌應用程序引擎ndb.Model轉換

我有以下ndb.Model類

class DateOfBirth(ndb.Model) 
    day = ndb.IntegerProperty() 
    month = ndb.IntegerProperty() 
    year = ndb.IntegerProperty() 

class User(ndb.Model): 
    dob = ndb.StructuredProperty(DateofBirth) 

而且在一個地方,當我在一個字典

user.dob = {"day": 12, "month": 10, "year": 1983}

它沒有抱怨過意外,看起來像它的工作。

這個預期,還是我預計今後會遇到的問題(因爲這種行爲不是記錄,並有望打破任何時間)

回答

2

這是一個讓我吃驚,我一直在使用NDB了長時間!但是從代碼,似乎它的目的是:https://github.com/GoogleCloudPlatform/datastore-ndb-python/blob/caac0c3e7dd4d9b2c6b32dfc5d59386dd02e6b57/ndb/model.py#L2354

它只會是一個小的改變你的代碼,不要有依賴,雖然行爲:

user.dob = DateOfBirth(**{"day": 12, "month": 10, "year": 1983}) 
+0

哇,我不知道這並! – marcadian

+1

這是打算。事實上,解開一個字典(或調用填充字典)是創建實體的簡單方法。 – janscas