1

我已經使用ndb對GAE中的數據結構進行了建模,這是「遞歸」的,因爲我希望它在其中存儲相同結構類型的實例。從概念上講,關於GAE中「遞歸結構屬性」的NameError ndb

class Person(ndb.Model): 
    name = ndb.StringProperty() 
    friend = ndb.StructuredProperty(Person) 

,我發現了以下錯誤:

Traceback (most recent call last): 
    File "C:\Program Files (x86)\Google\google_appengine\google\appengine\runtime\wsgi.py", line 239, in Handle 
    handler = _config_handle.add_wsgi_middleware(self._LoadHandler()) 
    File "C:\Program Files (x86)\Google\google_appengine\google\appengine\runtime\wsgi.py", line 298, in _LoadHandler 
    handler, path, err = LoadObject(self._handler) 
    File "C:\Program Files (x86)\Google\google_appengine\google\appengine\runtime\wsgi.py", line 84, in LoadObject 
    obj = __import__(path[0]) 
    File "C:\Users\Rusty\Documents\GitHub\AutoAddDrop\autoadddrop.py", line 2, in <module> 
    import models 
    File "C:\Users\Rusty\Documents\GitHub\AutoAddDrop\models.py", line 100, in <module> 
    class Bid(ndb.Model): 
    File "C:\Users\Rusty\Documents\GitHub\AutoAddDrop\models.py", line 123, in Bid 
    outbid_by = ndb.StructuredProperty(Bid) #Winning Bid 
NameError: name 'Bid' is not defined 

下面是models.py類:

class Bid(ndb.Model): 
    user_id = ndb.StringProperty() 
    league_id = ndb.StringProperty() 
    sport = ndb.StringProperty() 
    bid_amount = ndb.IntegerProperty() 
    timestamp = ndb.DateTimeProperty() 
    status = ndb.StringProperty() 
    target_player_id = ndb.StringProperty() 
    target_player_name = ndb.StringProperty() 
    target_player_team = ndb.StringProperty() 
    target_player_position = ndb.StringProperty() 
    add_player_id = ndb.StringProperty() 
    add_player_name = ndb.StringProperty() 
    add_player_team = ndb.StringProperty() 
    add_player_position = ndb.StringProperty() 
    drop_player_id = ndb.StringProperty() 
    drop_player_name = ndb.StringProperty() 
    drop_player_team = ndb.StringProperty() 
    drop_player_position = ndb.StringProperty() 
    bid_type = ndb.StringProperty() 
    bid_direction = ndb.StringProperty() 
    target_value = ndb.FloatProperty() 
    transaction_timestamp = ndb.DateTimeProperty() 
    outbid_by = ndb.StructuredProperty(Bid) #Winning Bid 
    outbid_by_key = ndb.KeyProperty(Bid) #key of winning bid 
    cbs_add_transaction = ndb.JsonProperty() 
    transaction_reason = ndb.StringProperty() 

回答

4

錯誤發生,因爲上課的時候身體執行出價類對象尚未創建。

您不能讓模型類包含同一類的子結構 - 這會導致無限的空間。 StructuredProperty 物理包括當前模型中的另一個模型的字段。

所以我建議刪除StructuredProperty行。

然後,您將在KeyProperty行上得到類似的錯誤,但對於KeyProperty,可以使用字符串('Bid')而不是直接類引用(Bid)來修復它。

您必須使用outbyd_by_key.get()來訪問出價的內容。

+0

感謝Guido - 這就是我的想法,但認爲如果可能的話,最好完全非規範化。有可能/你有什麼想法通過子類來做到這一點?即。 'Bid類(ndb.Model)'與類的所有肉類,然後'類Bid2(Bid)'只有屬性'outbid_by = ndb.StructuredProperty(Bid)' – rusty1042

+0

如果你能得到那個編譯我不'看看爲什麼它不應該工作。 :-) –