繁体   English   中英

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

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

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

NameError:未定义名称“联系人”。

任何帮助将不胜感激。

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

丹尼尔罗斯曼指出:

“因为它尚未定义。交换模型的顺序。”

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

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

class User(ndb.Model):
  pass

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

您必须交换2个模型的顺序,就像定义客户端模型时未定义Contact模型一样。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM