2010-08-17 71 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