2012-07-13 31 views
3

時,我有兩個關係:逆序使用_set.all

class Contact(models.Model): 
    first_name =models.CharField(max_length=30) 

class Activity(models.Model): 
    action  =models.CharField(max_length=200) 
    whom  =models.ForeignKey("Contact", null=True, blank=True) 

隨着

contacts=Contact.objects.get(slug=contactslug) 

我可以從我的URL請求調用的具體含量的不同

,另外

c = contacts.activity_set.all 

讓我打電話所有的活動。

我怎樣才能打電話的活動,但以相反的順序,因爲.order_by()reverse()似乎沒有工作。

回答

4

這可能會工作

reversed(contacts.activity_set.all()) 

但是,你應該這樣做:

class Activity(models.Model): 
    action  =models.CharField(max_length=200) 
    whom  =models.ForeignKey("Contact", null=True, blank=True) 

    class Meta: 
     ordering = ['action'] 

['-action']

+0

將'逆轉()'強制查詢集實例內部評估(因,像'django.db.models.query.QuerySet'實現中的所有非正統元類/重載的東西)? – fish2000 2012-07-13 11:17:09

+1

好反轉觸發器迭代所以是 – 2012-07-14 08:07:23

+0

很高興知道,thx。 – fish2000 2012-07-14 10:28:55