使用相关表字段的Django模型默认排序顺序 [英] Django model default sort order using related table field
本文介绍了使用相关表字段的Django模型默认排序顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以将模型的默认排序顺序设置为来自相关模型的字段(而不是整数键),即产生带有来自两个模型的字段的 SQL order by 子句的东西?如果是这样,如何?我可以通过 query_by 做到这一点,但我无法弄清楚如何默认设置它.谢谢.
Is it possible to set the default sort order for a model to a field from a related model (rather than the integer key) i.e. something that yields a SQL order by clause with a field from both models? If so, how? I can do this via query_by but I can't figure out how to set it by default. Thanks.
class Foo(models.Model):
name = models.CharField(max_length=50)
class Bar(models.Model):
related = models.ForeignKey(Foo)
bar_date = models.DateField()
class Meta:
ordering = ('bar_date', 'related.name', )
推荐答案
我使用 django 1.2.7,而不是连接 ForeignKey.Attribute 我们应该使用__",所以这段代码可以工作:
I use django 1.2.7 and instead of connecting ForeignKey.Attribute we should use "__", so this code will work:
class Meta:
ordering = ('bar_date', 'related__name', )
这篇关于使用相关表字段的Django模型默认排序顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文