2011-04-29 75 views
0

我使用递归外键创建以下类。问题和答案存储在同一个表中。
问题,类型=“Q”
答案类型=“A”以递归关系对相关记录进行排序

余万TOT排序在DESC,那里的依赖答案在ASC顺序来按日期排序的问题。我该怎么做在Django?

class Talk(models.Model): 
    user = models.ForeignKey(User) 
    destination = models.ForeignKey(Destination) 
    text = models.TextField() 
    type = models.CharField(max_length=30) 
    sup = models.ForeignKey('self', blank=True, null=True, related_name='child') 
    created_dt = models.DateTimeField(auto_now_add=True) 
    thumb_up = models.IntegerField() 
    thumb_down = models.IntegerField() 

class Meta: 
     ordering = ["-created_dt"] 

回答

0
questions = Talk.objects.filter(type='q') 

让你在默认排序的所有问题。为了得到整理特定问题的答案,可以说最新的一个,使用order_by

question = questions[0] 

sorted_answers = Talk.objects.filter(sup=question).order_by('created_dt') 

question.child.order_by('created_dt') 

它看起来是因为你用什么你related_name

+0

好笑,这似乎工作中,我有{%for talk_child in talk.child.all%}。似乎talk.child.order_by('created_dt')在视图中不受支持。有什么办法吗? – Elisa 2011-04-29 10:50:12