Using django-haystack how to sort results by content type

I use django-haystack for the search page on my site, and I want to order all the results by their content type. Is there a way I can do this? To keep things simple, suppose I have one application and several classes. thanks in advance

+4
source share
2 answers

from How to order search results by Model :

You can do SearchQuerySet().order_by('django_ct') . As a warning, this throws out relevance. The only way to maintain relevance and the group according to the model is either to launch a set of queries (one per model - usually due to a query caching) or to launch a query and after processing the results, rearrange them as you go.

from searchindex api :

Haystack reserves the following name field for internal use: id, django_ct, django_id, and content. Name and type names have been reserved, but no longer.

You can override these field names using HAYSTACK_ID_FIELD, HAYSTACK_DJANGO_CT_FIELD & HAYSTACK_DJANGO_ID_FIELD if necessary.

+4
source

Not sure what you mean by content type, but if you are talking about a model group, I have this job

  {% with page.object_list as results %} {% regroup results|dictsort:"verbose_name_plural" by verbose_name_plural as grouped_objects %} {% for ct in grouped_objects %} {{ ct.grouper }} {% for result in ct.list %} <p> <a href="{{ result.object.get_absolute_url }}">{{ result.object }}</a> </p> {% endfor %} {% empty %} <p>No results found.</p> {% endfor %} {% endwith %} 
+5
source

Source: https://habr.com/ru/post/1345032/


All Articles