Admin case-insensitive sorting question

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Admin case-insensitive sorting question

Mike Dewhirst-3
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

--
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 [hidden email].
To post to this group, send email to [hidden email].
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.
Reply | Threaded
Open this post in threaded view
|

RE: Admin case-insensitive sorting question

Matthew Pava
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,)

-----Original Message-----
From: [hidden email] [mailto:[hidden email]] On Behalf Of Mike Dewhirst
Sent: Wednesday, January 10, 2018 1:03 AM
To: Django users
Subject: Admin case-insensitive sorting question

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

--
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 [hidden email].
To post to this group, send email to [hidden email].
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.

--
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 [hidden email].
To post to this group, send email to [hidden email].
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/205b80f44a9f47569f5715825dc2c106%40ISS1.ISS.LOCAL.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Admin case-insensitive sorting question

Mike Dewhirst-3
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

> 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,)
>
> -----Original Message-----
> From: [hidden email] [mailto:[hidden email]] On Behalf Of Mike Dewhirst
> Sent: Wednesday, January 10, 2018 1:03 AM
> To: Django users
> Subject: Admin case-insensitive sorting question
>
> 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
>
> --
> 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 [hidden email].
> To post to this group, send email to [hidden email].
> 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.
>

--
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 [hidden email].
To post to this group, send email to [hidden email].
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/7f368bb3-34b8-cf48-e15b-a80fce2ea670%40dewhirst.com.au.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Admin case-insensitive sorting question

Mike Dewhirst-3
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

>
> Thanks
>
> Mike
>
>> 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,)
>>
>> -----Original Message-----
>> From: [hidden email]
>> [mailto:[hidden email]] On Behalf Of Mike Dewhirst
>> Sent: Wednesday, January 10, 2018 1:03 AM
>> To: Django users
>> Subject: Admin case-insensitive sorting question
>>
>> 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
>>
>> --
>> 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 [hidden email].
>> To post to this group, send email to [hidden email].
>> 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.
>>
>

--
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 [hidden email].
To post to this group, send email to [hidden email].
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/83a22a09-f6df-6525-02cc-ff26214fad4a%40dewhirst.com.au.
For more options, visit https://groups.google.com/d/optout.