user2738183
user2738183

Reputation:

google app engine computedProperty: when to use? When not to use?

When would using a ComputedProperty (ndb) in google app engine give you a distinct advantage over just computing when needed, on the backend (such as in a handler), without the datastore being involved?

Everything I'm reading seems to indicate that it's mostly useless, and would just slow queries down (at least the put operation if nothing else).

Thoughts?

I did see this:

"Note: Use ComputedProperty if the application queries for the computed value. If you just want to use the derived version in Python code, define a regular method or use Python's @property built-in."

but that doesn't really explain any advantage (why query if you can derive?)

Upvotes: 0

Views: 108

Answers (1)

Jaime Gomez
Jaime Gomez

Reputation: 7067

The documentation is quite clear on that regard, and i'll cite it again for reference, the Computed Properties section:

Note: Use ComputedProperty if the application queries for the computed value. If you just want to use the derived version in Python code, define a regular method or use Python's @property built-in.

When to use it? When you need to query some derived data, it needs to be written to the datastore so it gets indexed.

First example that came to mind: You're already storing the birthday of a user, but also need to filter by actual age, adding a property to derive that value might be the easiest and most efficient solution:

age = ndb.ComputedProperty(lambda self: calc_age(self.birthday))

Of course you could just have a function that returns the age, but that's only useful after you get the entity, can't use it for queries.

Upvotes: 3

Related Questions