sorting a ListField in mongoengine

ε祈祈猫儿з 提交于 2019-12-08 10:23:16

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!