如何在Google App Engine中定义具有多对多关系的模型?

发布于 2024-11-07 10:09:10 字数 837 浏览 1 评论 0原文

我正在编写一个 Google App Engine 应用程序,它具有具有多对多关系的数据模型。我想我做错了。我的数据模型类定义是:

class Project(db.Model):
    name = db.StringProperty()
    description = db.TextProperty()
    admin = db.ReferenceProperty(Appuser)
    website = db.LinkProperty()
    members = db.ListProperty(db.key, default=None)
    start_date = db.DateTimeProperty(auto_add_now = True)

class Appuser(db.Model):
    user_id = db.UserProperty()
    fullname = db.StringProperty()
    website = db.LinkProperty()
    involved_projects = db.ListProperty(db.key, default=None)
    current_project = db.ReferenceProperty(Project)

现在,每当我尝试运行它时,我都会收到一条错误,指出文件中未定义 Appuser 。发生这种情况是因为类 Appuser 是在 Project 之后定义的。我无法更改顺序,因为类 Appuser 也有一个类 Project 的 ReferenceProperty,我会收到 Project 未定义的错误。现在如何正确实施这一点。

I am writing a Google App Engine application which has a data model with multiple to multiple relationship. I think I am doing it wrong. My data model class definition is:

class Project(db.Model):
    name = db.StringProperty()
    description = db.TextProperty()
    admin = db.ReferenceProperty(Appuser)
    website = db.LinkProperty()
    members = db.ListProperty(db.key, default=None)
    start_date = db.DateTimeProperty(auto_add_now = True)

class Appuser(db.Model):
    user_id = db.UserProperty()
    fullname = db.StringProperty()
    website = db.LinkProperty()
    involved_projects = db.ListProperty(db.key, default=None)
    current_project = db.ReferenceProperty(Project)

Now whenever I try to run this I get an error stating Appuser is not defined in the file. It happens because the class Appuser is defined after Project. I couldn't change the order as class Appuser also has a ReferenceProperty to class Project I would get a not defined error for Project. Now how to implement this correctly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冰之心 2024-11-14 10:09:10

解决循环引用问题的一个快速解决方法是在 Project 中定义 admin 时删除referance_class(实际上并不需要验证)。

class Project(db.Model):
    admin = db.ReferenceProperty()

这并不理想,但应该可以解决问题。

A quick fix to the circular reference issue is to drop the referance_class when defining admin in Project (it is not required just a validation really).

class Project(db.Model):
    admin = db.ReferenceProperty()

It's not ideal, but should solve the issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文