おそらくこれは実際にはバグですが、それが知られていることを示唆するものが見つからないため、何か間違ったことをしていると思います.
日時フィールドと、優先順位を記述するオブジェクトへの外部キー (基本的には、番号で並べ替えて名前で表示できるように、名前と番号のペア) を持つモデル、Study があります。
最初にスタディ オブジェクトを優先度順に並べ替え (優先度の高い順にグループ化)、次に日時順、古い順に並べ替えたいと思います。このようにして、Stat がリストの一番上に表示され、古いものから順に表示されます。
sqlite である私のテスト DB を使用すると、これは予想どおりに機能します。
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)
一方、実稼働 DB (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 がこの順序で最初に「null」オブジェクトを選択することです。'priority__priority' は優先度の数値で、最高 = 最高です (この場合、null は無限大として扱われますが、sqlite では -infinity として扱われます)。それは大きな問題ではなく、手動でそれらを一番下に移動する面倒な回避策を実装するのに十分簡単です.
本当の問題は、日時がソートされていないように見えることです! 「低」優先度のオブジェクト内では、時間はマップ全体にあります。
これは ORM の何らかのバグである可能性がありますか、それとも sqlite では問題にならない特定の間違いをしているのでしょうか?