开发者

Check if a record exists in App Engine Datastore

开发者 https://www.devze.com 2023-02-22 23:01 出处:网络
From what I\'ve read, this is how I should check for any records... v = PC_Applications.all().filter(\'column =\', value)

From what I've read, this is how I should check for any records...

    v = PC_Applications.all().filter('column =', value)
if not v:
    return False

But this returns an error!

IndexError: The quer开发者_StackOverflowy returned fewer than 1 results

Any ideas to doing this? I've read that .count() is a bad option. I'm new to Python and App Engine so thank you for any patience!


if not v.get():

From App Engine, Query Class get()

Executes the query, then returns the first result, or None if the query returned no results.


This should work:

q = db.Query(PC_Applications, keys_only = True)
if not q.get():
    return false

I think .all().filter('column =', value) is even worse than .count, because it's not doing a keys-only query.


If you actually want to use the records, do something like:

results = PC_Applications.all().filter('column =', value).fetch(how_many_you_want)
if results:
    do_something_to_display_them()
else:
    do_something_else()
0

精彩评论

暂无评论...
验证码 换一张
取 消