Admin case-insensitive sorting question

128 views
Skip to first unread message

Mike Dewhirst

unread,
Jan 10, 2018, 2:03:24 AM1/10/18
to Django users
I have tried every which way to produce a case-insenstive list of
substances in the Admin including this:

def get_queryset(self, request):
    return super(SubstanceAdmin,
self).get_queryset(request).order_by(Lower('name').asc())

Which DOES work (as proven via print statement) but doesn't display that
way in the Admin which uses the natural ascii order. It is the
list_display feature which re-orders the substances.

I can get them sorting pseudo-insensitively by using a method to
uppercase the first char of the name and passing that method to
list_display.

However, that prevents the column order reversal feature which is a
function of the list_display feature.

I thought a case insensitive model manager might work until I saw in the
docs that subsequent order_by filtering will spoil it and I would still
have to pseudo_name them anyway.

So the question is ... should I bite the bullet and add a sort field to
the model and fill it with the substance name converted to lower-case?

Thanks

Mike

Matthew Pava

unread,
Jan 10, 2018, 9:24:25 AM1/10/18
to django...@googlegroups.com
If I'm understanding you correctly, you could use a callable in the list_display definition.
https://docs.djangoproject.com/en/2.0/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_display

def lower_case_name(obj):
return ("%s" % str(obj).lower()
lower_case_name.short_description = 'Substance'

class SubstanceAdmin(admin.ModelAdmin):
list_display = (lower_case_name,)
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/b1fc1d23-b0ab-d35e-9049-9a4f6091a4f7%40dewhirst.com.au.
For more options, visit https://groups.google.com/d/optout.

Mike Dewhirst

unread,
Jan 10, 2018, 6:54:38 PM1/10/18
to django...@googlegroups.com
On 11/01/2018 1:22 AM, Matthew Pava wrote:
> If I'm understanding you correctly, you could use a callable in the list_display definition.

That is what I'm doing. Unfortunately a callable for a list_display
column prevents sorting the list by that column.

I just thought of something. Maybe adjusting the model __str__() method
to always uppercase the first char of the name will work. I'll have to
back out all the adjustments so far and give it a try. I'll report back.

Thanks

Mike

Mike Dewhirst

unread,
Jan 10, 2018, 7:47:55 PM1/10/18
to django...@googlegroups.com
On 11/01/2018 10:53 AM, Mike Dewhirst wrote:
> On 11/01/2018 1:22 AM, Matthew Pava wrote:
>> If I'm understanding you correctly, you could use a callable in the
>> list_display definition.
>
> That is what I'm doing. Unfortunately a callable for a list_display
> column prevents sorting the list by that column.
>
> I just thought of something. Maybe adjusting the model __str__()
> method to always uppercase the first char of the name will work. I'll
> have to back out all the adjustments so far and give it a try. I'll
> report back.

Didn't work. I have decided to uppercase the first char of every
substance name on saving. Being able to sort by list columns beats using
a callable.

Thanks again

Mike
Reply all
Reply to author
Forward
0 new messages