Ryan
Ryan

Reputation: 127

Check if a record exists in App Engine Datastore

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 query 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!

Upvotes: 7

Views: 3701

Answers (3)

Wooble
Wooble

Reputation: 89857

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()

Upvotes: 0

Sean Fujiwara
Sean Fujiwara

Reputation: 4546

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.

Upvotes: 3

hyperslug
hyperslug

Reputation: 3623

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.

Upvotes: 7

Related Questions