问题
I have a model in mongoengine defined like this:
class Task(Document):
name = StringField(required=True, unique=True)
frequency = IntField(required=True)
quantity = IntField()
units = StringField()
events = ListField(DateTimeField(default=datetime.datetime.now))
How can I get the latest event
? I've tried the following to no success:
def latest(self):
return self.events.sort()[-1]
Instead of returning the events
sorted sort
returns None
回答1:
You could just use the Mongoengine SortedListField instead of ListField, here is the doc
Then you could simply return self.events
or its reverse if you wish as well
来源:https://stackoverflow.com/questions/45823538/sorting-a-listfield-in-mongoengine