2010-08-17 46 views
2

的打擊是我的數據庫:Django的訂單的問題

class TestCases(TmstafServerModel): 
    name = models.CharField(max_length=30) 

class TestRunSummary(TmstafServerModel): 
    testResult = models.ForeignKey(TestResult) 
    testCases = models.ForeignKey(TestCases) 
    platform = models.ForeignKey(Platform) 

我想通過測試用例的名字來獲得數據的順序,如:

all_fail_case = TestRunSummary.objects.all().order_by('testCases.name') 

,但它不能正常工作,我怎麼能得到的所有記錄TestRunSummary按testCases名稱排序? 謝謝:)

回答

2

使用__而不是.類似documentation中所述。或者,您也可以指定TestCase模型的默認排序,然後按testCases排序。

all_fail_case = TestRunSummary.objects.order_by('testCases__name').all() 
+0

謝謝!我可以問你爲什麼不使用TestRunSummary.objects.all()。order_by('testCases__name') 由於在訂單條件,所有() 謝謝你寫order_by()是正確的! – LoveTW 2010-08-17 05:35:50

+0

@袁 - 它是多餘的 - 「all」僅在需要確定具有「QuerySet」的情況下才有用(請參閱http://docs.djangoproject.com/en/dev/ref/models/querysets/#all上的文檔) – 2010-08-17 10:58:20

+0

非常感謝你!:) – LoveTW 2010-08-18 05:44:38