2

我有以下 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)

这不起作用,因为我TopicPost课堂上使用。我阅读了这个解决方案并解决了循环依赖问题。生成的模块如下:

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表,但它没有主题列。我怎么解决这个问题?

4

1 回答 1

2

这种方法不能正常工作,因为db.Model有一个元类在创建类时对 db 属性列表进行初始化工作。由于您的属性当时不存在,因此不会对其应用必要的初始化。

您可能需要考虑切换到 NDB,它通过对关键属性之一使用字符串名称来支持循环引用。

于 2012-10-29T15:09:06.007 回答