【发布时间】:2012-04-14 09:11:05
【问题描述】:
我有三个数据库表:
class Book(bd.Model):
title = db.StringProperty()
pub_time = db.DateTimeProperty()
subject = db.StringProperty()
class Author(db.Model):
name = db.StringProperty()
age = db.IntegerProperty()
class Match(db.Model):
bk = ReferenceProperty(Book, collection_name='book')
ath = ReferenceProperty(Author, collection_name='books_written')
问题:我想过滤作者ATH写的书,主题SUB
我的方法:
class BookSearch(webapp2.requestHandler):
def post(self):
b = Books.all().filter("subject =", 'SUB')
a = Author.all().filter("name =", "ATH")
ret = Match.all().filter("bk =", b). filter("ath =", a)
self.response.out.write(ret.count())
但这不起作用,我得到一个错误:
BadValueError: Unsupported type for property : <class 'google.appengine.ext.db.Query'>
【问题讨论】:
-
在您的 Book 模型中添加
author = ReferenceProperty(Author)不是更简单吗?这样以后你就可以做一个Book.all().filter("subject", 'SUB').filter("author", author_key).fetch()? -
感谢您的建议,但是,对不起@alex 但是您为什么忘记一本书可以有多个作者?一个作者可以写多本书。
-
对,抱歉,没看到。但是,只需使用ListProperty 然后:
authors = ListProperty(Key)很简单。我猜这个查询是一样的。 -
顺便说一句,我强烈建议您使用新库 NDB,它可以让您执行类似
author = KeyProperty('Author', repeated=True)的操作 -
这是我想在我的项目中实现的最后一件事,已经完成和预先填充的事情太多了,我可能会在我的下一个项目中看看。
标签: python google-app-engine webapp2