Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if Google App Engine datastore entity has a particular property

I am new with developing web applications with Google App Engine.

I wanted to check for entities in my datastore which have a null value set for a list property (db.ListProperty). However, when I tried to check anything against the entity.list GAE gives the error:

'super' object has no attribute 'list'.

After some search I saw in this SO question that setting an empty value for a list property of a GAE datastore entity is equivalent to not setting the property at all. This explains my error.

So I need to match entities in the datastore which do not have any list property set at all. Looking at the GAE documentation I still haven't found any method which allows me to check whether an entity has a particular property set.

NOTE: I don't have to do this with GQL. I can retrieve all entities with GQL, then check with python. But a GQL solution is also fine.

like image 570
Abhranil Das Avatar asked Dec 03 '22 03:12

Abhranil Das


2 Answers

I can think of three ways around this.

You're probably running into this situation because originally your model did not have a 'list' attribute, and you added one later, so you might have older instances in the datastore without the 'list' attribute. You can write a mapreduce function that goes through your datastore and ensures all entities have the 'list' element.

Otherwise, you can use the python hasattr function

if hasattr(entity, 'list'):
    a = entity.list[0] # or whatever operation you want

Thirdly, you can use an exception handler to catch the error case.

like image 178
dragonx Avatar answered Apr 08 '23 04:04

dragonx


There is no way to do this in GQL, which can only filter on existing values. Instead, you should use some form of computed property. NDB supports these, or you can override the _pre_put hook to have some other property set to be, for example, the length of the list. This will allow you to query on this new property == 0.

like image 20
mjibson Avatar answered Apr 08 '23 04:04

mjibson