Google App Engine (python): filter users based on custom fields

I use the webapp2_extras.appengine.auth.models.User service, which basically extends google.appengine.api.users model . Now I have user users registered in my application, and they have many custom fields. The problem is that I want to filter / (multifilter) of all users using various custom fields. For example:

the user model has 2 fields is_active and activation_key now I want to filter them using these fields, for example:

 from google.appengine.api import users act_key = 'hw-2j38he63u83hd6hak3FshSqj3TGemn9' user = users.all().filter('is_active =', False).filter('activation_key =', act_key).get() if user: return True else: return False 

What are the best ways to filter in a custom model using custom fields?

Edit:

Also tried the following:

 from webapp2_extras.appengine.auth.models import User query = User.query().filter('is_active =', False) print query 

but this causes an error as follows:

 Traceback (most recent call last): File "/opt/google_appengine_1.6.4/google/appengine/ext/admin/__init__.py", line 320, in post exec(compiled_code, globals()) File "<string>", line 6, in <module> File "lib/ndb/query.py", line 968, in filter raise TypeError('Cannot filter a non-Node argument; received %r' % arg) TypeError: Cannot filter a non-Node argument; received 'is_active =' 
+1
google-app-engine filter
source share
3 answers

webapp2_extras.appengine.auth.models.User - webapp2_extras.appengine.auth.models.User model with created , updated , auth_ids and password properties. Anything outside this or user properties will be stored as an opaque blob, i.e. it will not be indexed. However, they cannot be queried / filtered.

You can subclass the User model and add the properties you need or (better) create a new model (say Account ).

This is a good habit when working with a data warehouse so that your model is as small as possible, and that is why: consider a model with dozens of properties, every time you get the entity that you need to get them; the network is not a problem here, that is, protobuff decoding and Python are actually not very good.

+1
source share

The syntax for querying Expando properties in NDB is slightly different from how it is done in the old DB API, and is documented here . Your query should look something like this:

 user = users.all().filter(ndb.GenericProperty('is_active') == False, ndb.GenericProperty('activation_key) == act_key).get() 
+6
source share

Is your user user model implemented with db or ndb ?

for the first question, if you only need to check if the user exists or not, you can make a request key_only .all(keys_only=True).filter(...

The error you encounter when querying with the User WebApp2 model is dependent on the ndb package. with ndb, the correct syntax for the request is:

 User.query().filter(User.is_active == False) 

or

 User.query(User.is_active == False) 

Of course, you will need to request a custom user model.

+1
source share

All Articles