我有两节课:
class Foo(models.Model):
date = models.DateTimeField(auto_now_add = True)
...
class Bar(models.Model):
foo = models.OneToOneField(
Foo,
on_delete = models.CASCADE,
primary_key = True
)
...
如何订购Bar's
对象,如下所示:
query_set = Bar.objects.all().order_by('-foo.date')
答案 0 :(得分:2)
您可以通过__
访问外键值
像这样
query_set = Bar.objects.all().order_by('-foo__date')