Reputation: 717
To avoid computing and setting fields explicitly on a mongoengine Document
, I'd like to have it as a computed field. Is this currently possible?
Here's a MWE of what I'm expecting:
class Task(Document):
meta = {"collection": "tasks"}
@property
def get_values_count(self):
return len(self.values)
dateAdded = DateTimeField()
dateStarted = DateTimeField(default=datetime.utcnow())
values = ListField(IntField())
values_count = IntField(default=get_values_count) # Either this, normal class functions not supported by Documents
# values_count = IntField(default = lambda : len(self.values)) //Or this (this won't compile)
Any way of achieving something like this?
Upvotes: 0
Views: 672
Reputation: 2484
If I correctly understand, you don't want to store values_count
in your database. So you can just use a @property
class Task(Document):
meta = {"collection": "tasks"}
@property
def values_count(self):
return len(self.values)
dateAdded = DateTimeField()
dateStarted = DateTimeField(default=datetime.utcnow())
values = ListField(IntField())
Upvotes: 1