Ralf17 Ralf17 - 9 months ago 42
Python Question

Query a boolean property in NDB Python

I have a Greeting model

class Greeting(ndb.Model):
author = ndb.StructuredProperty(Author)
content = ndb.TextProperty(indexed=False)
avatar = ndb.BlobProperty()
date = ndb.DateTimeProperty(auto_now_add=True)
public = ndb.BooleanProperty(default=False)

wherein I use the following code to query seven Greeting posts at each page in Python Google App Engine:

posts_query = Greeting.query(
curs = Cursor(urlsafe=self.request.get('cursor'))
posts,next_curs, more = posts_query.fetch_page(7, start_cursor=curs)

I wanted it to show only posts to that has public modified into True so I changed it into

posts_query = Greeting.query(
ancestor=session_key(session_name), Greeting.public == True).order(-Greeting.date) #line changed
curs = Cursor(urlsafe=self.request.get('cursor'))
posts,next_curs, more = posts_query.fetch_page(7, start_cursor=curs)

However, it is giving me an error:

File "/home/ralf/Desktop/google_projects/website/views/events.py", line 28
Greeting.public == True).order(-Greeting.date)
SyntaxError: non-keyword arg after keyword arg

How can I fix this? What is the appropriate code for this kind of query? Help is greatly appreciated.

P.S. As you can see I am also using a query cursor.

Answer Source

I fixed it by using this

posts_query = Greeting.query(Greeting.public == True).order(-Greeting.date)

instead of

posts_query = Greeting.query(ancestor=session_key(session_name),
        Greeting.public == True).order(-Greeting.date)

Yes, reversing the order of ancestor=session_key(session_name) and Greeting.public == True in the query function removes the errors. However, I found that my answer is a good fix considering that it is a part of a feature I added in my website.

ancestor=session_key(session_name) is not needed at all for public users of the app. They just view the page and does not login at all.