以递归关系对相关记录进行排序
我有以下带有递归外键的课程。问题和答案存储在同一个表中。
问题,输入='q'
答案类型 = 'a'
我想按 DESC 中的日期对问题进行排序,其中相关答案必须按 ASC 顺序排序。我该怎么做?
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"]
I have following class with recursive foreign key. Questions and Answers storing in same table.
Question, type='q'
Answer type = 'a'
I wan tot sort questions by date in DESC, where as dependent answer has to be sorted in ASC order. How can I do in 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"]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为您提供默认顺序中的所有问题。要获得特定问题的答案(例如最新的问题),请使用
order_by
:或者
由于您用于
lated_name
的内容而看起来很有趣gets you all the question in your default ordering. To get the answers sorted for a specific question, lets say the newest one, use
order_by
:or
Which looks funny because of what you used for your
related_name