需要帮助设置ndb.StructuredProperty属性一夫一妻的关系

2024-04-27 20:22:58 发布

您现在位置:Python中文网/ 问答频道 /正文

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

名称错误:未定义名称“Contact”。你知道吗

任何帮助都将不胜感激。你知道吗

主.py

from dataObjects import *

数据对象.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()

Tags: py名称model属性错误contact联系人phone
3条回答

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

class User(ndb.Model):
  pass

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

必须交换这两个模型的顺序,因为在定义客户机模型时,没有定义联系人模型。你知道吗

正如Daniel Roseman指出的:

“因为它还没有定义。交换模型的顺序。”

基本上,在创建模型客户机时,您的代码需要一个Contact对象。因为你的代码不存在联系人,所以它会中断。你知道吗

相关问题 更多 >