我使用drf创建API视图,如下所示:
{
"name": "some name",
"attribute1": "some attribute",
"attribute2": "some attribute",
"attribute3": "some attribute",
"attribute4": "some attribute",
"stats": {
"status": false,
"count": 0
}
}
stats
字段被创建为序列化程序(模型中的OneToOneField),但有时在这个OneToOne关系中没有关联的对象,因此stats
就是null
。在
问题是我想按f.e.stats__count
订购我的查询集。这种排序的升序是有效的。count = 0
在begging,null在末尾,但是当我想按降序排序时,我希望在begging处有这个属性的最大值。我得到了所有的零分。在
问题是:如何将这些元素与stats = null
放在结果的末尾,或者至少从queryset中删除它们(但仅限于从stats
按字段排序时)。在
最好的办法是使用annotate(),如下面的问题所述:Django : Order by position ignoring NULL
相关问题 更多 >
编程相关推荐