0

我有三个数据库表:

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'>
4

2 回答 2

1

a 和 b 是查询而不是实体。您需要先获取实体,然后才能将其用作另一个查询中的过滤器:

class BookSearch(webapp2.requestHandler):
    def post(self):
        b = Books.all().filter("subject =", 'SUB').get()
        a = Author.all().filter("name =", "ATH").get()
        ret = Match.all().filter("bk =", b).filter("ath =", a)
        self.response.out.write(ret.count())
于 2012-04-14T13:04:10.187 回答
-1

您应该使用bk IN而不是,bk =因为结果b可以是多个单个值。

试试这个:

ret = Match.all().filter("bk IN", b). filter("ath IN", a)

文档在这里https://developers.google.com/appengine/docs/python/datastore/queryclass#Query_filter

于 2012-04-14T09:15:09.020 回答