[Django]-Detect row difference (view or model)?

1πŸ‘

βœ…

The regroup built in filter can do this for you without annotating your objects in the view. As the documentation says, it’s kind of complicated.

https://docs.djangoproject.com/en/dev/ref/templates/builtins/#regroup

{% regroup all_publications by date.year as year_list %}
{% for year in year_list %}
  <h1>{{ year.grouper }}</h1>
  {% for publication in year.list %}
    <li>{{ publication.title }}</li>
  {% endfor %}
{% endfor %}

1πŸ‘

I think you want the regroup template tag;

{% regroup all_publications by date as publication_groups %}
<ul>
{% for publication_group in publication_groups %}
    <li>{{ publication_group.grouper }}
    <ul>
        {% for publication in publication_group.list %}
          <li>{{ publication.title }}</li>
        {% endfor %}
    </ul>
    </li>
{% endfor %}
</ul> 

1πŸ‘

Maybe the template tag regroup could help.

Alternatively, you could do this grouping by year in the view function (will try to provide code later).

Leave a comment