简体   繁体   English

如何使用mongoengine在booleanfield上使用order_by

[英]How to use order_by on booleanfield using mongoengine

I want to order the result of my mongoengine call on two different fields. 我想在两个不同的字段上订购mongoengine调用的结果。

  1. Open. 打开。 This has status True or False. 状态为True或False。 I want this because I want to display the open questions first 我想要这个,因为我想先显示未解决的问题
  2. Opendate. Opendate里。 Because I want to newest questions to show on top. 因为我想将最新的问题放在最前面。

Combined this should create a list where I can see on top the open questions (ordered by creation date) and than the already closed questions, also ordered by creationdate. 结合起来,应该会创建一个列表,在其中我可以在顶部看到未解决的问题(按创建日期排序),然后可以查看已经关闭的问题(也按创建日期排序)。

The code I started with is: 我开始的代码是:

To call the API: 调用API:

questions = Questions.questions_of_user

To handle the call: 处理呼叫:

@queryset_manager
def questions_of_user(doc_cls, queryset):
    return queryset.filter(questioner=current_user.id).order_by('-openDate')

My first suggestion was to just add 'status' to the order_by (maybe with or without + or - ) would do it. 我的第一个建议是仅将“状态”添加到order_by(可能带有+或不带有+或-)即可。 But so far no luck. 但是到目前为止没有运气。

Than I tried to only order by the open field, because I thought I was just making an mistake combining the two. 比起我尝试只按照开放领域进行订购,是因为我认为结合这两者只是在犯错误。 So I got this: 所以我得到了这个:

@queryset_manager
def questions_of_user(doc_cls, queryset):
    return queryset.filter(questioner=current_user.id).order_by('-open')

That however did not work as well. 但是,这样效果不佳。 I hope someone can help me out. 我希望有人能帮助我。 Thanks in advance 提前致谢

可以在queryset的order_by方法中传递多个键

queryset.filter(questioner=current_user.id).order_by('-open', '-openDate')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM