Shedokan
Shedokan

Reputation: 1202

Is it possible to only have a ComputedProperty for certain entities?

In my application I have a model like so:

class MyModel(ndb.Model):
    entity_key_list = ndb.KeyProperty('k', repeated=True, indexed=False)
    entity_key_num = ndb.ComputedProperty('n', lambda self: len(self.entity_key_list))
    verified = ndb.BooleanProperty('v')

Is it possible to have the entity_key_num property when verified is false?

Upvotes: 1

Views: 50

Answers (1)

Lipis
Lipis

Reputation: 21835

You can return None if not verified like this:

entity_key_num = ndb.ComputedProperty('n', lambda self: len(self.entity_key_list) if not self.verified else None)

If you don't want to have the value None at all and dynamically delete or create this property then you will have to use the ndb.Expando class where you can do all these fancy stuff. Note that you won't be able to delete the ComputedProperty so you will have to keep track of that value on your own.

Upvotes: 2

Related Questions