我有以下带有递归外键的类。存储在同一表格中的问题和答案
问题,类型='q'
答案类型='a'
我想在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 :(得分: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