1

我正在尝试通过以下方式从我的选择中排除具有名为 meta 的标签的帖子:

meta_id = db(db.tags.name == "meta").select().first().id
not_meta = ~db.posts.tags.contains(meta_id)
posts=db(db.posts).select(not_meta)

但是这些帖子仍然出现在我的选择中。

写那个表达式的正确方法是什么?

我的表看起来像:

db.define_table('tags',
    db.Field('name', 'string'),
    db.Field('desc', 'text', default="")
)

db.define_table('posts', 
    db.Field('title', 'string'),
    db.Field('message', 'text'),
    db.Field('tags', 'list:reference tags'),
    db.Field('time', 'datetime', default=datetime.utcnow())
)

我在 GAE 上使用 Web2Py 1.99.7,在 Python 2.7.2 上使用 High Replication DataStore

更新:

我只是posts=db(not_meta).select()按照@Anthony 的建议进行了尝试,但它给了我一张带有以下 Traceback 的票:

Traceback (most recent call last):
  File "E:\Programming\Python\web2py\gluon\restricted.py", line 205, in restricted
    exec ccode in environment
  File "E:/Programming/Python/web2py/applications/vote_up/controllers/default.py", line 391, in <module>
  File "E:\Programming\Python\web2py\gluon\globals.py", line 173, in <lambda>
    self._caller = lambda f: f()
  File "E:/Programming/Python/web2py/applications/vote_up/controllers/default.py", line 8, in index
    posts=db(not_meta).select()#orderby=settings.sel.posts, limitby=(0, settings.delta)
  File "E:\Programming\Python\web2py\gluon\dal.py", line 7578, in select
    return adapter.select(self.query,fields,attributes)
  File "E:\Programming\Python\web2py\gluon\dal.py", line 3752, in select
    (items, tablename, fields) = self.select_raw(query,fields,attributes)
  File "E:\Programming\Python\web2py\gluon\dal.py", line 3709, in select_raw
    filters = self.expand(query)
  File "E:\Programming\Python\web2py\gluon\dal.py", line 3589, in expand
    return expression.op(expression.first)
  File "E:\Programming\Python\web2py\gluon\dal.py", line 3678, in NOT
    raise SyntaxError, "Not suported %s" % first.op.__name__
SyntaxError: Not suported CONTAINS

更新 2:

由于~目前没有使用 Datastore 处理 GAE,因此我使用以下方法作为临时解决方法:

meta = db.posts.tags.contains(settings.meta_id)
all=db(db.posts).select()#, limitby=(0, settings.delta)
meta=db(meta).select()
posts = []
i = 0
for post in all:
    if i==settings.delta: break
    if post in meta: continue
    else:
        posts.append(post)
        i += 1
#settings.delta is an long integer to be used with limitby
4

1 回答 1

1

尝试:

meta_id = db(db.tags.name == "meta").select().first().id
not_meta = ~db.posts.tags.contains(meta_id)
posts = db(not_meta).select()

首先,您的初始查询返回一个完整的 Row 对象,因此您只需提取“id”字段。其次,not_meta是一个 Query 对象,因此它在内部db(not_meta)创建一个 Set 对象,定义要选择的记录集(该select()方法需要为每条记录返回一个字段列表,以及一些其他参数,例如orderby,groupby等.)。

于 2012-04-12T04:59:56.640 回答