【问题标题】:NameError on "recursive StructuredProperty" in GAE ndbGAE ndb 中“递归结构化属性”上的 NameError
【发布时间】:2016-09-17 12:27:52
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

    发生错误是因为类体执行时还没有创建Bid类对象。

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

    所以我建议删除 StructuredProperty 行。

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

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

    【讨论】:

    • 谢谢 Guido - 这就是我的想法,但如果可能的话,最好完全非规范化。有可能/您对通过子类化做这件事有什么想法? IE。 class Bid(ndb.Model) 包含类的所有内容,然后 class Bid2(Bid) 仅包含属性 outbid_by = ndb.StructuredProperty(Bid)
    • 如果你可以编译它,我不明白为什么它不应该工作。 :-)
    猜你喜欢
    • 2019-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-29
    相关资源
    最近更新 更多