Thank you Geoff for the reply.

>>>How about creating a function inside protein, e.g. protein.href, that
returns the proper URL?
Let me try this method also.

>>>(If that trailing star is necessary, you could also have a function to
generate that, e.g. protein.starname.)
Yes. The trailing star is necessary. To display to the users it is a mutant
protein. I will use protein.starname.

I use the below sort function to sort the object. Source:
https://stackoverflow.com/a/58925845

def alphanumeric_sort(objects_list, sort_key):
    """ Sort a list of objects by a given key
    This function sort a list of objects by a given
    key common across the objects
    Sorting can be implemented on keys that are either
    alphabets, integers or both
    """
    convert = lambda text: int(text) if text.isdigit() else text
    alphanum_key = lambda key: [
        convert(c) for c in re.split("([0-9]+)", getattr(key, sort_key))
    ]
    return sorted(objects_list, key=alphanum_key)


I assume I found the issue. My mutation protein database contains greek
letters also. That seems to be the problem. The rest of the data sorts
well. I have to sort unicode strings also.

regards
Kannan

On Sun, Sep 12, 2021 at 3:32 PM Geoff Kuenning <[email protected]> wrote:

> How about creating a function inside protein, e.g. protein.href, that
> returns the proper URL?  Then you could write <a href={{ protein.href }}>{{
> protein.name }}</a> in the template.  (If that trailing star is
> necessary, you could also have a function to generate that, e.g.
> protein.starname.)
>
> On Saturday, September 11, 2021 at 10:17:10 AM UTC-7 [email protected]
> wrote:
>
>> Hello everyone,
>>
>> I have a model that contains alphanumeric protein names e.g.
>> ('Ypp1Aa100"). I have to properly sort it when returning the contents to
>> the html. I found some fantastic sorting examples from this blog post
>> https://blog.codinghorror.com/sorting-for-humans-natural-sort-order/. I
>> have a sorting function in the view that sorts well and returns the
>> contents to the html. This works really well for my template tag display.
>>
>> However, depending upon the proteins (mutant or not) I need a different
>> href. Hence, I have used a function in the django model as below
>>
>>     def mutant(self):
>>         proteins = ProteinDatabase.objects.all()
>>         mutants = MutantProteinDatabase.objects.all()
>>         for protein in proteins:
>>             if self.name == protein.name:
>>                 return False
>>         for mutant in mutants:
>>             if self.name == mutant.name:
>>                 return True
>>         return None
>>
>> Then, I updated the template tag like below
>>
>>            {% if protein.mutant %}
>>             <td align="left"><a href=/mutant_detail/{{ protein.name  }}
>> >{{ protein.name }}*</a></td>
>>             {% elif protein.mutant != None %}
>>             <td align="left"><a href=/protein_detail/{{ protein.name  }}
>> > {{ protein.name }} </a></td>
>>            {% else %}
>>             <td> {{ protein.name }} </td>
>>            {% endif %}
>>
>> However, my sorting doesn't work in the above template tags (due to if
>> else). If I use only one href it sorting works perfectly well.
>>
>> To sum it up. I have a model named ActivityDatabase. In this model I have
>> to check whether it is mutant or not from the other two databases
>> (ProteinDatabase and MutantDatabase). If it is a mutant protein it has to
>> have a different href (/protein_detail/ and /mutant_detail/). In addition ,
>> I have a natural sort function in the views that sorts by alphanumeric
>> proteins.  I also use DataTables js to display my contents (for extra
>> detailed information)
>> https://datatables.net/examples/api/row_details.html. There is also
>> natural sorting in the datatables example which doesn't suit my needs.
>>
>> Can you please suggest some pointers to think of any other logic for this
>> problem? I use Django 3.2.
>>
>> Kannan
>>
>>
>>
>> --
> 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 [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/b1ed83fb-3d22-4efe-97fb-6f6688b50756n%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/b1ed83fb-3d22-4efe-97fb-6f6688b50756n%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CADiZb_c3JCivgNmxzqoU8w8rz%3D7Q0D%3DsbMW4EFn_Nj0Pe9KzrQ%40mail.gmail.com.

Reply via email to