Admin ordering works incorrectly if ModelAdmin.ordering has several fields, and one of them is an another Model field
|Reported by:||semenov||Owned by:||nobody|
|Severity:||Keywords:||newforms-admin admin ordering|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
If I create two dependent models, and ask the admin interface to order instances of the dependent Model based on the field of the base Model AND on its own field, the secondary ordering is lost. See minimalistic example:
class Foo(models.Model): is_active = models.BooleanField(default=True) class Bar(models.Model): foo = models.OneToOneField(Foo, primary_key=True) name = models.CharField(max_length=100) def __unicode__(self): return "%s, %s" % (self.name, "active" if self.foo.is_active else "inactive") # ... class BarAdmin(admin.ModelAdmin): list_display = ('name',) ordering = ('-foo__is_active', 'name') admin.site.register(Bar, BarAdmin)
insert into app_foo set is_active=1; insert into app_foo set is_active=1; insert into app_foo set is_active=1; insert into app_bar values (1,'aaaa'); insert into app_bar values (1,'ccc'); insert into app_bar values (1,'bbb');
If we run a manual query, it works as expected:
>>> Bar.objects.order_by('-foo__is_active', 'name') [<Bar: aaaa, active>, <Bar: bbb, active>, <Bar: ccc, active>]
However, the admin page at http://my.site/admin/app/bar/ would display:
aaa ccc bbb
which means the ordering was lost.
If both fields are in the same model, everything works fine.
Django version is .