1👍
I think you are approaching this from the wrong angle. Logic like that should go in the views.
You seem to wanting to group your products by type, so wherever you specify your queryset in your view for the products, use order by.
queryset = Products.objects.filter(filters_here_if_you_need=any).order_by("type__option")
Then in your template you will only need the inner loop
{% for sth in products %}
//Do something
{% endfor %}
Now your products will be ordered by the options.
Or am I missing something?
0👍
There’s a lot of options actually
{% for thing in things %}
{% for product in products.all %}
{% if thing.type == product.type %}
<!-- do stuff -->
{% else %}
<!-- pass -->
{% endif %}
{% endfor %}
{% end for %}
or
{% for thing in things %}
{% for product in products.all %}
{% ifequal thing.type product.type %}
<!-- do stuff -->
{% else %}
<!-- pass -->
{% endifequal %}
{% endfor %}
{% end for %}
You could write a SimpleTag to filter your related manager directly in your template, but I wouldn’t recommend it. You could also add a property to either of the models that takes an argument and use a simple tag to feed the method in your template.
If your data isn’t that dynamic you could also directly return the proper context to your template.
Source:stackexchange.com