Opened 16 years ago

Closed 16 years ago

Last modified 16 years ago

#8682 closed (invalid)

Admin ordering works incorrectly if ModelAdmin.ordering has several fields, and one of them is an another Model field

Reported by: Ilya Semenov Owned by: nobody
Component: contrib.admin Version: dev
Severity: Keywords: newforms-admin admin ordering
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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 [8696].

Change History (4)

comment:1 by Karen Tracey, 16 years ago

Resolution: invalid
Status: newclosed

I think it's coincidence that the ordering works as you want when the fields are in the same model, the Admin only uses one field for ordering. See http://docs.djangoproject.com/en/dev/ref/contrib/admin/#ordering and #7942. Closing as invalid since the docs state this doesn't work.

comment:2 by Ilya Semenov, 16 years ago

Yeah, sorry, overlooked that in the documenation.

Then, however, I don't understand what's the point to have ModelAdmin.ordering as a tuple, if only the first element is always used?

comment:3 by Karen Tracey, 16 years ago

Replying to semenov:

Then, however, I don't understand what's the point to have ModelAdmin.ordering as a tuple, if only the first element is always used?

Consistency with most of the other ModelAdmin options? Leaving the door open to one day implement multi-field ordering in Admin? I don't really know.

comment:4 by Jacob, 16 years ago

Consistancy, sure, but also because although the current UI doesn't support multiple ordering, a future UI might. Worth leaving the door open.

Note: See TracTickets for help on using tickets.
Back to Top