[Solved]-Haystack search results: how to use different templates for different models in page.object_list?


Ok, duh, immediately after asking this I dug deeper into the Haystack docs, and found exactly what I need:

{% ifequal result.model_name 'apple' %}
  Apple: {{ result.object.titlefield_for_apple }}
{% else %}
  Orange: {{ result.object.otherfield_for_orange }}
{% endifequal %}

Which totally makes sense, because of course Haystack should pass the types back with the results. It actually passes several versions of the type info, for easy use in the template:

  • model_name – The model’s name.
  • model – The model class.
  • verbose_name – A prettier version of the model’s class name for display.

Leave a comment