1

エンティティのクエリを実行したいが、結果に含めたくない一連のキー/ID を除外します。これを行う最善の方法は何ですか?

おそらく .IN 演算子が役立つだろうと思っていましたが、その方法がわかりませんでした。

そこで、単一のキーの除外をチェーンする次のソリューションを使用しました。

q = models.Comment.query()
for exclude_key in list_of_comment_keys_to_exclude:
  q = q.filter( models.Comment.key != exclude_key )
q = q.order( models.Comment.key ) # without this: BadRequestError: The first sort property must be the same as the property to which the inequality filter is applied.
q = q.order( models.Comment.creationTime )

これは機能しているように見えますが、それについてうまくいく方法はありますか?

4

1 に答える 1

7

それはうまくいくかもしれませんが、かなり非効率的です。すべての結果を取得した後で、ユーザー コード内の個々のキーを除外する方が安価です。例えば:

q = models.Comment.query().order(...)
results = [res for res in q.fetch() if res.key not in list_of_comment_keys_to_exclude]
于 2013-01-08T19:00:28.787 に答える