1

我在 GAE 中使用“递归”的 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

1 回答 1

4

发生该错误的原因是类主体执行时尚未创建 Bid 类对象。

您不能让模型类包含同一类的子结构——这将导致无限空间。StructuredProperty在物理上包括当前模型中另一个模型的字段。

所以我建议删除 StructuredProperty 行。

然后,您将在 KeyProperty 行上遇到类似的错误,但对于 KeyProperty,它可以通过使用字符串 ('Bid') 而不是直接类引用 (Bid) 来修复。

您必须使用 outbyd_by_key.get() 来访问投标的内容。

于 2013-09-27T20:08:01.507 回答