Thursday, July 30, 2020

Re: Admin list sorting problem

Apologies for lack of proof reading; the example code should be:


    def _name(self):
        return '%s' % self.name
    _name.admin_order_field = 'sort_name'


On Friday, 31 July 2020 08:21:45 UTC+2, Derek wrote:
I've had to do something similar to handle species.

I'd suggest breaking "pure" database design and creating a new field - say, "sort_name".  This is created "on save" (obviously you can run a script to quickly generate this field's values for all existing records).  You don't show "sort_name" on the admin interface; what you then do is create a new attribute on the chemical model - call it "_name"; this displays the actual chemical name, but the sort is set to your new field.  Something like:

    def _name(self):
        return '%s' % self.name
    _name.admin_order_field = 'display_name'

In the Admin, only show "_name" and not "sort_name" or "name".

HTH
Derek

On Thursday, 30 July 2020 08:51:01 UTC+2, Mike Dewhirst wrote:
I have looked closely at the Admin docs and the page source and I think
I'm at the blank wall.

I have a collection of 14,000+ chemical names which just naturally sort
weirdly. This is because scientists insist on incuding "locants" in
chemical names. Locants indicate where sub-molecules sit in the actual
whole molecule. That isn't a sufficiently scientific description but
with the following example should suffice to decribe my problem.

2,4-Toluene diisocyanate
2,4,6-tris(dimethylaminomethyl)phenol
2,6-Toluene diisocyanate

Django wants to sort this alphanumerically but scientists don't want to
see it that way.

I wrote a chemsort algorithm and added a sort field (slug) to the model
and used the model's Meta class ordering to sort the chemical according
to slug.

2,4,6-tris(dimethylaminomethyl)phenol----->>
dimethylaminomethylphenol246tris
... (many thousands more chemicals) ...
2,4-Toluene diisocyanate----->> toluenediisocyanate24
2,6-Toluene diisocyanate ----->> toluenediisocyanate26

This works fine until in the Admin, on clicking the substance name in
the heading everything reverts to sorting on the name instead of the slug.

This is understandable because, sensibly, the Admin appears to use
javascript to handle such resorting in the browser instead of making a
round trip to the database via the server. Therefore, I think I need
slug in the list for an in-browser solution.

I would like to include the slug in the Admin but severely reduce the
allocated real-estate to just one or two characters.

How can I do that? Maybe there is another solution?

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 django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/4fc11cfc-4dec-45e7-aa19-768e7c5b2a9fo%40googlegroups.com.

No comments:

Post a Comment