Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check null value for UserProperty in Google App Engine

In Google App Engine, datastore modelling, I would like to ask how can I check for null value of a property with class UserProperty? for example: I have this code:

class Entry(db.Model):
  title = db.StringProperty()
  description = db.StringProperty()
  author = db.UserProperty()
  editor = db.UserProperty()
  creationdate = db.DateTimeProperty()

When I want to check those entries that have the editor is not null, I can not use this kind of GqlQuery

query = db.GqlQuery("SELECT * FROM Entry " +
                    "WHERE editor IS NOT NULL" +
                                    "ORDER BY creationdate DESC")
    entries = query.fetch(5)

I am wondering if there is any method for checking the existence of a variable with UserProperty? Thank you!

like image 285
Hoang Pham Avatar asked Aug 09 '09 20:08

Hoang Pham


1 Answers

query = db.GqlQuery("SELECT * FROM Entry WHERE editor > :1",None)

However, you can't ORDER BY one column and have an inequality condition on another column: that's a well-known GAE limitation and has nothing to do with the property being a UserProperty nor with the inequality check you're doing being with None.

Edit: I had a != before, but as @Nick pointed out, anything that's != None is > None, and > is about twice as fast on GAE (since != is synthesized by union of < and >), so using > here is a worthwhile optimization.

like image 170
Alex Martelli Avatar answered Nov 07 '22 03:11

Alex Martelli