需要帮助设置 ndb.StructuredProperty 一对多关系

Need help setting up ndb.StructuredProperty for One-to-Many Relationship

我正在使用 Python (2.7) 和 GAE 创建一个应用程序。我正在尝试创建一对多关系。有一位拥有众多房产的客户也有很多潜在的联系人。联系人也有各种属性。使用 ndb.StructuredProperty 的示例看起来非常简单,但是当我使用结构化的 属性 行导入数据模型时,我的日志中不断出现以下错误:

NameError: 名称 'Contact' 未定义。

如有任何帮助,我们将不胜感激。

main.py

from dataObjects import *

dataObjects.py

class Client(ndb.Model):
    createDate = ndb.DateProperty()
    name = ndb.StringProperty()
    address1 = ndb.StringProperty()
    address2 = ndb.StringProperty()
    state = ndb.StringProperty()
    zipCode = ndb.StringProperty()
    phone = ndb.StringProperty()
    fax = ndb.StringProperty()
    website = ndb.StringProperty()
    city = ndb.StringProperty()
    industry = ndb.StringProperty()
    status = ndb.StringProperty()
    notes = ndb.StringProperty()
    financing = ndb.StringProperty()
    contacts = ndb.StructuredProperty(Contact, repeated=True)

class Contact(ndb.Model):
    firstName = ndb.StringProperty()
    lastName = ndb.StringProperty()
    role = ndb.StringProperty()
    status = ndb.StringProperty()
    phone = ndb.StringProperty()
    fax = ndb.StringProperty()
    email = ndb.StringProperty()
    createDate = ndb.DateProperty()
    isClient = ndb.StringProperty()
    address = ndb.StringProperty()

正如Daniel Roseman指出的那样:

"Because it's not defined yet. Swap the order of the models."

基本上,在创建模型客户端时,您的代码需要一个联系人对象。由于您的代码不存在联系人,因此它中断了。

此外,对于无法仅更改定义顺序的情况,您可以在定义模型(包括自引用)后添加 属性:

class User(ndb.Model):
  pass

User.friends = ndb.StructuredProperty(User, repeated=True)
User._fix_up_properties()

您必须调换 2 个模型的顺序,因为当您定义了 Client 模型时未定义 Contact 模型。