Opened 7 years ago

Closed 7 years ago

Last modified 7 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: semenov Owned by: nobody
Component: contrib.admin Version: master
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: UI/UX:

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 Changed 7 years ago by kmtracey

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to invalid
  • Status changed from new to closed

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 Changed 7 years ago by semenov

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 Changed 7 years ago by kmtracey

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 Changed 7 years ago by jacob

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