Xavier
Xavier

Reputation: 33

How to filter for an empty list in google app engine

I have a field in my model which is a StringListProperty. The default value is an empty list: []. Is there a way to filter for all entities with an empty list under this field?

Upvotes: 1

Views: 85

Answers (1)

Dmytro Sadovnychyi
Dmytro Sadovnychyi

Reputation: 6201

No. You have to create another property like isListEmpty with boolean value so you can filter by it. If you use ndb, you could use ComputedProperty for this.

class Foo(ndb.Model):
    my_list = ndb.StringProperty(repeated=True)
    is_list_empty = ndb.ComputedProperty(lambda self: True if not self.my_list else False)

UPDATE: Actually, I think you could check if that list is empty on each put, and if it is -- populate it with some constant value like ['__EMPTY__'] so you can query it later without introducing another property.

Upvotes: 2

Related Questions