stoebelj
stoebelj

Reputation: 1598

sorting a ListField in mongoengine

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

Upvotes: 1

Views: 1048

Answers (1)

Sreenadh T C
Sreenadh T C

Reputation: 611

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

Upvotes: 2

Related Questions