Hi everyone,
I’ve encountered an issue with a test case that passes in Django version 2.2 but fails when I upgrade to Django 4.2. The test verifies correct regrouping of data in a Django template. Here’s a brief overview of the application, test setup and problem:
- Application overview: The application is an open-source foodbank campaign management system, the source is available on GitHub at mrts/foodbank-campaign. It’s designed to facilitate the organization and management of foodbank food collection campaigns, including managing shop locations where the food is collected and scheduling volunteer shifts in these locations.
- Test setup: The test involves creating a
Campaign
and relatedCampaignLocationShift
objects and using a Django template to regroup and render the data into a JavaScript variableshopsAndShifts
. The template uses the{% regroup %}
tag to organize location shifts by district and then by location. The code is here: campaigns/test.py. - Problem: Upon upgrading to Django 4.2, this test case fails. It seems the
{% regroup %}
tag or related queryset handling is different. Here’s the failure:
AssertionError: var shopsAndShifts = { 3: [ { shop: 'Rocca Al Mare Prisma (Tallinn)', [truncated]... != var shopsAndShifts = { 3: [ { shop: 'Haabersti Rimi', shifts: [ { pk: [truncated]...
In the Django 4.2 version the data is messed up so that the resulting JavaScript variableshopsAndShifts
contains only a single location after being interpreted by the browser. Here’s how it is actually used in the application.
Question: Has anyone else encountered similar issues with regrouping when upgrading to Django 4+? Should I report this as a bug?
Django versions:
- Django version before upgrade: 2.2.28
- Django version after upgrade: 4.2.11
The template code for reference:
{% regroup locations_and_shifts by location.district as district_list %}
var shopsAndShifts = {
{% for district in district_list %}
{{ district.grouper.id }}: [
{% regroup district.list by location as location_shifts %}
{% for location in location_shifts %}
{
shop: '{{ location.grouper.name }}',
shifts: [
{% for shift in location.list %}
{
pk: '{{ shift.pk }}',
when: '{{ shift.day }} {{ shift.start }} - {{ shift.end }}',
freePlaces: {{ shift.free_places|default_if_none:shift.total_places }}
}{% if not forloop.last %},{% endif %}
{% endfor %}
]
}{% if not forloop.last %},{% endif %}
{% endfor %}
]{% if not forloop.last %},{% endif %}
{% endfor %}
};
I’ve already reviewed the Django release notes and documentation but haven’t pinpointed the exact cause of this issue. Thank you in advance for any insights or advice you can provide!