如何在Google App Engine中定义具有多对多关系的模型?
我正在编写一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决循环引用问题的一个快速解决方法是在
Project
中定义 admin 时删除referance_class(实际上并不需要验证)。这并不理想,但应该可以解决问题。
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).It's not ideal, but should solve the issue.