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', )
Take a look at order-with-respect-to.
As an alternative to order_with_respect_to (which only supports one field), you can use a custom manager to provide the ordering. This also allows you to order on multiple fields in Foo and to still have a normal Bar.objects manager. You'll have to test to see if the Meta.ordering or the custom manager ordering is applied first.
class FooSortedManager(models.Manager):
def get_query_set(self):
return super(FooSortedManager, self).get_query_set().order_by('foo__name')
class Foo(models.Model):
name = models.CharField(max_length=50)
class Bar(models.Model):
related = models.ForeignKey(Foo)
bar_date = models.DateField()
foo_sorted = FooSortedManager()
class Meta:
ordering = ('bar_date',)
In modern version of django it is:
class Meta:
ordering = ['word']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With