简体   繁体   English

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

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

I am creating an application using Python (2.7) and GAE. 我正在使用Python(2.7)和GAE创建一个应用程序。 I am trying to create a one-to-many relationship. 我正在努力建立一对多的关系。 There is one client with numerous properties which also has many potential contacts. 有一个客户有很多属性,也有很多潜在的联系人。 Contacts also has various properties. 联系人也有各种属性。 The example of using the ndb.StructuredProperty seems pretty straight forward, but when I import my data model with a structured property line, I keep getting the following error in my log: 使用ndb.StructuredProperty的例子看起来非常简单,但是当我使用结构化属性行导入我的数据模型时,我的日志中不断出现以下错误:

NameError: Name 'Contact' is not defined. NameError:未定义名称“联系人”。

Any help would be greatly appreciated. 任何帮助将不胜感激。

main.py main.py

from dataObjects import *

dataObjects.py 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()

As Daniel Roseman pointed out : 丹尼尔罗斯曼指出:

"Because it's not defined yet. Swap the order of the models." “因为它尚未定义。交换模型的顺序。”

Basically, when creating the model Client, your code needs a Contact object. 基本上,在创建模型客户端时,您的代码需要一个Contact对象。 Since Contact doesn't exist for your code, it breaks. 由于您的代码不存在联系人,因此它会中断。

Also, for cases when its not possible to just change the order of definition you can add a property after the model were defined (that includes self-referencing): 此外,对于无法仅更改定义顺序的情况,您可以在定义模型后添加属性(包括自引用):

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