我有以下python模块:
from google.appengine.ext import db
from user import User
class Post(db.Model):
content = db.TextProperty()
time = db.DateTimeProperty(auto_now_add=True)
user = db.ReferenceProperty(User)
topic = db.ReferenceProperty(Topic)
class Forum(db.Model):
parent_ = db.SelfReferenceProperty()
name = db.StringProperty()
description = db.StringProperty()
topics_count = db.IntegerProperty(default=0)
posts_count = db.IntegerProperty(default=0)
last_post = db.ReferenceProperty(Post)
class Topic(db.Model):
forum = db.ReferenceProperty(Forum)
title = db.StringProperty()
replies_count = db.IntegerProperty(default=0)
views = db.IntegerProperty(default=0)
time = db.DateTimeProperty(auto_now_add=True)
user = db.ReferenceProperty(User)
last_post = db.ReferenceProperty(Post)
这不起作用,因为我在Topic
课程中使用Post
。我读了this solution并解决了循环依赖问题。生成的模块如下:
from google.appengine.ext import db
from user import User
class Post(db.Model):
content = db.TextProperty()
time = db.DateTimeProperty(auto_now_add=True)
user = db.ReferenceProperty(User)
class Forum(db.Model):
parent_ = db.SelfReferenceProperty()
name = db.StringProperty()
description = db.StringProperty()
topics_count = db.IntegerProperty(default=0)
posts_count = db.IntegerProperty(default=0)
last_post = db.ReferenceProperty(Post)
class Topic(db.Model):
forum = db.ReferenceProperty(Forum)
title = db.StringProperty()
replies_count = db.IntegerProperty(default=0)
views = db.IntegerProperty(default=0)
time = db.DateTimeProperty(auto_now_add=True)
user = db.ReferenceProperty(User)
last_post = db.ReferenceProperty(Post)
Post.topic = db.ReferenceProperty(Topic)
但是现在我还有另外一个问题。 App Engine创建Post
表,但它没有主题列。我怎么解决这个问题?
答案 0 :(得分:2)
这种方法无法正常工作,因为db.Model
有一个元类,它在创建类时对db属性列表进行初始化工作。由于您的财产当时不在那里,因此不会对其进行必要的初始化。
您可能需要考虑切换到NDB,它通过使用其中一个关键属性的字符串名称来支持循环引用。