也许这实际上是一个错误,但由于找不到任何表明它已知的东西,我会假设我做错了什么。
我有一个模型,Study,它有一个日期时间字段以及一个描述优先级的对象的外键(基本上是一个名称/数字对,这样可以按数字排序并按名称查看)。
我想首先按优先级对研究对象进行排序(因此首先将它们分组为更高优先级),然后按日期时间,最旧的优先。这样,Stat 将位于列表的顶部,最旧的在前,依此类推。
使用我的测试数据库,它是 sqlite,这就像预期的那样工作:
ordered = ordered = models.Study.objects.all().order_by('arrived').order_by('-priority__priority')
for study in ordered:
print(study.arrived, study.priority)
(datetime.datetime(2013, 5, 15, 23, 22, tzinfo=<UTC>), <StudyPriority: STAT>)
(datetime.datetime(2013, 5, 15, 23, 20, 51, 948639, tzinfo=<UTC>), <StudyPriority: LOW>)
(datetime.datetime(2013, 5, 15, 23, 21, 6, 674582, tzinfo=<UTC>), <StudyPriority: LOW>)
(datetime.datetime(2013, 5, 15, 23, 21, 21, 86984, tzinfo=<UTC>), <StudyPriority: LOW>)
(datetime.datetime(2013, 5, 15, 23, 21, 36, 234965, tzinfo=<UTC>), <StudyPriority: LOW>)
(datetime.datetime(2013, 5, 15, 23, 21, 59, 618850, tzinfo=<UTC>), <StudyPriority: LOW>)
(datetime.datetime(2013, 5, 15, 23, 22, 18, 991499, tzinfo=<UTC>), <StudyPriority: LOW>)
(datetime.datetime(2013, 5, 15, 23, 22, 26, 229715, tzinfo=<UTC>), <StudyPriority: LOW>)
(datetime.datetime(2013, 5, 15, 23, 22, 31, 150896, tzinfo=<UTC>), <StudyPriority: LOW>)
(datetime.datetime(2013, 5, 15, 23, 22, 35, 379259, tzinfo=<UTC>), <StudyPriority: LOW>)
(datetime.datetime(2013, 5, 15, 23, 22, 43, 207465, tzinfo=<UTC>), <StudyPriority: LOW>)
(datetime.datetime(2013, 5, 15, 23, 31, 42, 176697, tzinfo=<UTC>), None)
另一方面,使用生产数据库(postgres),事情......出错了:
ordered = models.Study.objects.all().order_by('arrived').order_by('-priority__priority')
for study in ordered:
print(study.arrived, study.priority)
(datetime.datetime(2013, 5, 29, 22, 31, 45, tzinfo=<UTC>), None)
(datetime.datetime(2013, 5, 29, 22, 36, 15, tzinfo=<UTC>), <StudyPriority: STAT>)
(datetime.datetime(2013, 5, 29, 22, 36, 20, 520912, tzinfo=<UTC>), <StudyPriority: LOW>)
(datetime.datetime(2013, 5, 29, 22, 35, 18, 784721, tzinfo=<UTC>), <StudyPriority: LOW>)
(datetime.datetime(2013, 5, 29, 22, 35, 44, 540762, tzinfo=<UTC>), <StudyPriority: LOW>)
(datetime.datetime(2013, 5, 29, 22, 35, 51, 355645, tzinfo=<UTC>), <StudyPriority: LOW>)
(datetime.datetime(2013, 5, 29, 22, 35, 56, 800284, tzinfo=<UTC>), <StudyPriority: LOW>)
(datetime.datetime(2013, 5, 29, 22, 36, 2, 190325, tzinfo=<UTC>), <StudyPriority: LOW>)
(datetime.datetime(2013, 5, 29, 22, 36, 15, 137803, tzinfo=<UTC>), <StudyPriority: LOW>)
(datetime.datetime(2013, 5, 29, 22, 31, 44, 759514, tzinfo=<UTC>), <StudyPriority: LOW>)
(datetime.datetime(2013, 5, 29, 22, 37, 52, 264583, tzinfo=<UTC>), <StudyPriority: LOW>)
(datetime.datetime(2013, 5, 29, 22, 37, 54, 191852, tzinfo=<UTC>), <StudyPriority: LOW>)
(datetime.datetime(2013, 5, 29, 22, 37, 56, 385968, tzinfo=<UTC>), <StudyPriority: LOW>)
(datetime.datetime(2013, 5, 29, 22, 37, 57, 865427, tzinfo=<UTC>), <StudyPriority: LOW>)
(datetime.datetime(2013, 5, 29, 22, 38, 1, 959433, tzinfo=<UTC>), <StudyPriority: LOW>)
(datetime.datetime(2013, 5, 29, 22, 38, 4, 748306, tzinfo=<UTC>), <StudyPriority: LOW>)
(datetime.datetime(2013, 5, 29, 22, 36, 57, 562198, tzinfo=<UTC>), <StudyPriority: LOW>)
(datetime.datetime(2013, 5, 29, 22, 34, 37, 909631, tzinfo=<UTC>), <StudyPriority: LOW>)
最明显的烦恼是,无论出于何种原因,postgres 都会按此顺序首先选择“空”对象。'priority__priority' 是数值优先级值,最高 = 最高(因此在这种情况下,null 被视为无穷大,而在 sqlite 中,它被视为 -infinity)。这不是一个大问题,很容易实现一个烦人的解决方法来手动将它们移动到底部。
真正的问题是日期时间似乎没有排序!在“低”优先级对象中,时间遍布整个地图。
这可能是 ORM 中的某种错误,还是我在做一些可识别的错误,而这恰好不是 sqlite 的问题?