Search code examples
pythongoogle-app-enginegoogle-cloud-platformgoogle-cloud-datastoreapp-engine-ndb

Using keywords argument to query in GAE ndb


I met some problems when I would like to query the ndb on GAE.

Is it possible to query the db using keywords argument?

From the document, it shows that the query string should be expression.

qry = Account.query(username == 'test_user')

Can I query or filter by a keyword argument like

search_userinfo(username='test_user')

def search_userinfo(self, **kwargs):
    return UserInfo.query(**kwargs)

If not, how can I transfer the keyword argument into the expression that match ndb's condition.

Thank you.


Solution

  • Something like this will turn your kwargs into filters for the query:

    def search_userinfo(**kwargs):
      qry = UserInfo.query(*(getattr(UserInfo, k)==v for (k,v) in kwargs.items()))
      return qry