views:

413

answers:

1

Hello, 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!

+5  A: 
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.

Alex Martelli
In this case, "WHERE editor > :1" would be a better choice - a != query gets translated into two queries - one for less-than, and one for greater-than. Since nothing is less than None, it makes sense to skip it.
Nick Johnson
Thank you Alex and Nick!!! Your answers solved my problem!
sfa
Great tip Nick, tx -- editing to fix.
Alex Martelli
@Hoang, always happy to help, but remember to accept an answer that solves your problem -- that's basic SO etiquette!
Alex Martelli
hihi, sorry guys, I am new to stackoverflow :D just tried to use this website 19 days ago with two questions ;) it really helps ;)
sfa