GAE ndb中“递归StructuredProperty”的NameError

时间:2013-09-27 15:21:31

标签: google-app-engine google-cloud-datastore app-engine-ndb

我使用“递归”的ndb在GAE中建模了一个数据结构,因为我希望它在其中存储相同结构化类型的实例。从概念上讲,

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()

1 个答案:

答案 0 :(得分:4)

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

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

所以我建议删除StructuredProperty行。

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

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