View source on GitHub |
A Property
whose value is determined by a user-supplied function.
Inherits From: GenericProperty
, Property
, ModelAttribute
, expected_type
google.appengine.ext.ndb.ComputedProperty(
func, name=None, indexed=None, repeated=None, verbose_name=None
)
Computed properties cannot be set directly, but are instead generated by a
function when required. They are useful to provide fields in Cloud Datastore
that can be used for filtering or sorting without having to manually set the
value in code — for example, sorting on the length of a BlobProperty
, or
using an equality filter to check if another field is not empty.
ComputedProperty
can be declared as a regular property, passing a function
as the first argument, or it can be used as a decorator for the function that
does the calculation.
Example:
>>> class DatastoreFile(Model):
... name = StringProperty()
... name_lower = ComputedProperty(lambda self: self.name.lower())
...
... data = BlobProperty()
...
... @ComputedProperty
... def size(self):
... return len(self.data)
...
... def _compute_hash(self):
... return hashlib.sha1(self.data).hexdigest()
... hash = ComputedProperty(_compute_hash, name='sha1')
Args | |
---|---|
func
|
A function that takes one argument, the model instance, and returns a calculated value. |
Methods
IN
IN(
value
)
Comparison operator for the IN
comparison operator.
The Python IN
operator cannot be overloaded in the way we want
to, so we define a method. For example:
Employee.query(Employee.rank.IN([4, 5, 6]))
Note that the method is called ._IN()
but may normally be invoked
as .IN()
; ._IN()
is provided for the case you have a
StructuredProperty
with a model that has a Property named IN
.
__eq__
__eq__(
value
)
Return a FilterNode
instance representing the =
comparison.
__ge__
__ge__(
value
)
Return a FilterNode
instance representing the >=
comparison.
__gt__
__gt__(
value
)
Return a FilterNode
instance representing the >
comparison.
__le__
__le__(
value
)
Return a FilterNode
instance representing the <=
comparison.
__lt__
__lt__(
value
)
Return a FilterNode
instance representing the <
comparison.
__ne__
__ne__(
value
)
Return a FilterNode
instance representing the !=
comparison.
__neg__
__neg__()
Return a descending sort order on this Property.
For example:
Employee.query().order(-Employee.rank)
__pos__
__pos__()
Returns an ascending sort order on this property.
Note that this is redundant but provided for consistency with
__neg__
. For example, the following two are equivalent:
Employee.query().order(+Employee.rank)
Employee.query().order(Employee.rank)