#35198 closed Bug (fixed)
Facet filters crash for queryset with no ID.
Reported by: | Simon Alef | Owned by: | Shafiya Adzhani |
---|---|---|---|
Component: | contrib.admin | Version: | 5.0 |
Severity: | Release blocker | Keywords: | |
Cc: | Thomas Feldmann, Sarah Boyce | Triage Stage: | Ready for checkin |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Description:
Our Django application defines a relational model where Customer entities have a one-to-many relationship with Contracts, and Contract entities have a one-to-many relationship with Rental Items. A custom admin filter (RentalItemsFilter) was implemented on the Customer model to enable filtering based on the presence of active Rental Items.
Issue:
Using the "show counts" feature within the RentalItemsFilter on the Customer admin page to display counts of devices (Rental Items) associated with each customer through contracts triggers an exception.
Steps to Reproduce:
Load fixtures from fixture.json
Go to the Django admin dashboard and navigate to the Customer model section.
Click on "Show counts".
Traceback: Request Method: GET Request URL: http://127.0.0.1:8000/admin/demo/customer/?_facets=True Django Version: 5.0.2 Python Version: 3.11.7 Installed Applications: ['django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'demo'] Installed Middleware: ['django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware'] Template error: In template /django_bug_report/.venv/lib/python3.11/site-packages/django/contrib/admin/templates/admin/change_list.html, error at line 87 no such column: demo_rentalitem.id 77 : <h2>{% translate 'Filter' %}</h2> 78 : {% if cl.is_facets_optional or cl.has_active_filters %}<div id="changelist-filter-extra-actions"> 79 : {% if cl.is_facets_optional %}<h3> 80 : {% if cl.add_facets %}<a href="{{ cl.remove_facet_link }}" class="hidelink">{% translate "Hide counts" %}</a> 81 : {% else %}<a href="{{ cl.add_facet_link }}" class="viewlink">{% translate "Show counts" %}</a>{% endif %} 82 : </h3>{% endif %} 83 : {% if cl.has_active_filters %}<h3> 84 : <a href="{{ cl.clear_all_filters_qs }}">✖ {% translate "Clear all filters" %}</a> 85 : </h3>{% endif %} 86 : </div>{% endif %} 87 : {% for spec in cl.filter_specs %} {% admin_list_filter cl spec %} {% endfor %} 88 : </div> 89 : {% endif %} 90 : {% endblock %} 91 : </div> 92 : </div> 93 : {% endblock %} 94 : Traceback (most recent call last): File "/django_bug_report/.venv/lib/python3.11/site-packages/django/db/backends/utils.py", line 105, in _execute return self.cursor.execute(sql, params) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/db/backends/sqlite3/base.py", line 329, in execute return super().execute(query, params) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ The above exception (no such column: demo_rentalitem.id) was the direct cause of the following exception: File "/django_bug_report/.venv/lib/python3.11/site-packages/django/core/handlers/exception.py", line 55, in inner response = get_response(request) ^^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/core/handlers/base.py", line 220, in _get_response response = response.render() ^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/template/response.py", line 114, in render self.content = self.rendered_content ^^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/template/response.py", line 92, in rendered_content return template.render(context, self._request) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/template/backends/django.py", line 61, in render return self.template.render(context) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/template/base.py", line 171, in render return self._render(context) ^^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/template/base.py", line 163, in _render return self.nodelist.render(context) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/template/base.py", line 1000, in render return SafeString("".join([node.render_annotated(context) for node in self])) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/template/base.py", line 1000, in <listcomp> return SafeString("".join([node.render_annotated(context) for node in self])) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/template/base.py", line 961, in render_annotated return self.render(context) ^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/template/loader_tags.py", line 159, in render return compiled_parent._render(context) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/template/base.py", line 163, in _render return self.nodelist.render(context) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/template/base.py", line 1000, in render return SafeString("".join([node.render_annotated(context) for node in self])) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/template/base.py", line 1000, in <listcomp> return SafeString("".join([node.render_annotated(context) for node in self])) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/template/base.py", line 961, in render_annotated return self.render(context) ^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/template/loader_tags.py", line 159, in render return compiled_parent._render(context) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/template/base.py", line 163, in _render return self.nodelist.render(context) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/template/base.py", line 1000, in render return SafeString("".join([node.render_annotated(context) for node in self])) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/template/base.py", line 1000, in <listcomp> return SafeString("".join([node.render_annotated(context) for node in self])) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/template/base.py", line 961, in render_annotated return self.render(context) ^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/template/loader_tags.py", line 65, in render result = block.nodelist.render(context) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/template/base.py", line 1000, in render return SafeString("".join([node.render_annotated(context) for node in self])) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/template/base.py", line 1000, in <listcomp> return SafeString("".join([node.render_annotated(context) for node in self])) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/template/base.py", line 961, in render_annotated return self.render(context) ^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/template/loader_tags.py", line 65, in render result = block.nodelist.render(context) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/template/base.py", line 1000, in render return SafeString("".join([node.render_annotated(context) for node in self])) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/template/base.py", line 1000, in <listcomp> return SafeString("".join([node.render_annotated(context) for node in self])) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/template/base.py", line 961, in render_annotated return self.render(context) ^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/template/defaulttags.py", line 326, in render return nodelist.render(context) ^^^^^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/template/base.py", line 1000, in render return SafeString("".join([node.render_annotated(context) for node in self])) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/template/base.py", line 1000, in <listcomp> return SafeString("".join([node.render_annotated(context) for node in self])) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/template/base.py", line 961, in render_annotated return self.render(context) ^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/template/defaulttags.py", line 242, in render nodelist.append(node.render_annotated(context)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/template/base.py", line 961, in render_annotated return self.render(context) ^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/template/library.py", line 237, in render output = self.func(*resolved_args, **resolved_kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/contrib/admin/templatetags/admin_list.py", line 505, in admin_list_filter "choices": list(spec.choices(cl)), ^^^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/contrib/admin/filters.py", line 150, in choices facet_counts = self.get_facet_queryset(changelist) if add_facets else None ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/contrib/admin/filters.py", line 87, in get_facet_queryset return filtered_qs.aggregate( File "/django_bug_report/.venv/lib/python3.11/site-packages/django/db/models/query.py", line 604, in aggregate return self.query.chain().get_aggregation(self.db, kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/db/models/sql/query.py", line 615, in get_aggregation result = compiler.execute_sql(SINGLE) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/db/models/sql/compiler.py", line 1562, in execute_sql cursor.execute(sql, params) ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/db/backends/utils.py", line 122, in execute return super().execute(sql, params) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/db/backends/utils.py", line 79, in execute return self._execute_with_wrappers( File "/django_bug_report/.venv/lib/python3.11/site-packages/django/db/backends/utils.py", line 92, in _execute_with_wrappers return executor(sql, params, many, context) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/db/backends/utils.py", line 100, in _execute with self.db.wrap_database_errors: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/db/utils.py", line 91, in __exit__ raise dj_exc_value.with_traceback(traceback) from exc_value ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/db/backends/utils.py", line 105, in _execute return self.cursor.execute(sql, params) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/db/backends/sqlite3/base.py", line 329, in execute return super().execute(query, params) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Exception Type: OperationalError at /admin/demo/customer/ Exception Value: no such column: demo_rentalitem.id
Attachments (2)
Change History (15)
by , 9 months ago
Attachment: | django_bug_report.zip added |
---|
comment:1 by , 9 months ago
Cc: | added |
---|
comment:2 by , 9 months ago
Cc: | added |
---|---|
Severity: | Normal → Release blocker |
Summary: | Django Admin "Show counts" Feature Triggers Exception in a Custom Filter → Facet filters crash for queryset with no ID. |
Triage Stage: | Unreviewed → Accepted |
Thanks for the report. Regression in 868e2fcddae6720d5713924a785339d1665f1bb9.
comment:3 by , 9 months ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
Will try to look up and fix this.
by , 9 months ago
Attachment: | screenshot-related-list-filter.png added |
---|
Screenshot of Related List Filter on reported apps
comment:4 by , 9 months ago
Replying to Simon Alef:
Description:
Our Django application defines a relational model where Customer entities have a one-to-many relationship with Contracts, and Contract entities have a one-to-many relationship with Rental Items. A custom admin filter (RentalItemsFilter) was implemented on the Customer model to enable filtering based on the presence of active Rental Items.
Issue:
Using the "show counts" feature within the RentalItemsFilter on the Customer admin page to display counts of devices (Rental Items) associated with each customer through contracts triggers an exception.
Steps to Reproduce:
Load fixtures from fixture.json
Go to the Django admin dashboard and navigate to the Customer model section.
Click on "Show counts".
Traceback: Request Method: GET Request URL: http://127.0.0.1:8000/admin/demo/customer/?_facets=True Django Version: 5.0.2 Python Version: 3.11.7 Installed Applications: ['django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'demo'] Installed Middleware: ['django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware'] Template error: In template /django_bug_report/.venv/lib/python3.11/site-packages/django/contrib/admin/templates/admin/change_list.html, error at line 87 no such column: demo_rentalitem.id 77 : <h2>{% translate 'Filter' %}</h2> 78 : {% if cl.is_facets_optional or cl.has_active_filters %}<div id="changelist-filter-extra-actions"> 79 : {% if cl.is_facets_optional %}<h3> 80 : {% if cl.add_facets %}<a href="{{ cl.remove_facet_link }}" class="hidelink">{% translate "Hide counts" %}</a> 81 : {% else %}<a href="{{ cl.add_facet_link }}" class="viewlink">{% translate "Show counts" %}</a>{% endif %} 82 : </h3>{% endif %} 83 : {% if cl.has_active_filters %}<h3> 84 : <a href="{{ cl.clear_all_filters_qs }}">✖ {% translate "Clear all filters" %}</a> 85 : </h3>{% endif %} 86 : </div>{% endif %} 87 : {% for spec in cl.filter_specs %} {% admin_list_filter cl spec %} {% endfor %} 88 : </div> 89 : {% endif %} 90 : {% endblock %} 91 : </div> 92 : </div> 93 : {% endblock %} 94 : Traceback (most recent call last): File "/django_bug_report/.venv/lib/python3.11/site-packages/django/db/backends/utils.py", line 105, in _execute return self.cursor.execute(sql, params) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/db/backends/sqlite3/base.py", line 329, in execute return super().execute(query, params) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ The above exception (no such column: demo_rentalitem.id) was the direct cause of the following exception: File "/django_bug_report/.venv/lib/python3.11/site-packages/django/core/handlers/exception.py", line 55, in inner response = get_response(request) ^^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/core/handlers/base.py", line 220, in _get_response response = response.render() ^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/template/response.py", line 114, in render self.content = self.rendered_content ^^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/template/response.py", line 92, in rendered_content return template.render(context, self._request) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/template/backends/django.py", line 61, in render return self.template.render(context) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/template/base.py", line 171, in render return self._render(context) ^^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/template/base.py", line 163, in _render return self.nodelist.render(context) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/template/base.py", line 1000, in render return SafeString("".join([node.render_annotated(context) for node in self])) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/template/base.py", line 1000, in <listcomp> return SafeString("".join([node.render_annotated(context) for node in self])) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/template/base.py", line 961, in render_annotated return self.render(context) ^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/template/loader_tags.py", line 159, in render return compiled_parent._render(context) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/template/base.py", line 163, in _render return self.nodelist.render(context) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/template/base.py", line 1000, in render return SafeString("".join([node.render_annotated(context) for node in self])) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/template/base.py", line 1000, in <listcomp> return SafeString("".join([node.render_annotated(context) for node in self])) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/template/base.py", line 961, in render_annotated return self.render(context) ^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/template/loader_tags.py", line 159, in render return compiled_parent._render(context) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/template/base.py", line 163, in _render return self.nodelist.render(context) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/template/base.py", line 1000, in render return SafeString("".join([node.render_annotated(context) for node in self])) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/template/base.py", line 1000, in <listcomp> return SafeString("".join([node.render_annotated(context) for node in self])) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/template/base.py", line 961, in render_annotated return self.render(context) ^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/template/loader_tags.py", line 65, in render result = block.nodelist.render(context) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/template/base.py", line 1000, in render return SafeString("".join([node.render_annotated(context) for node in self])) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/template/base.py", line 1000, in <listcomp> return SafeString("".join([node.render_annotated(context) for node in self])) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/template/base.py", line 961, in render_annotated return self.render(context) ^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/template/loader_tags.py", line 65, in render result = block.nodelist.render(context) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/template/base.py", line 1000, in render return SafeString("".join([node.render_annotated(context) for node in self])) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/template/base.py", line 1000, in <listcomp> return SafeString("".join([node.render_annotated(context) for node in self])) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/template/base.py", line 961, in render_annotated return self.render(context) ^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/template/defaulttags.py", line 326, in render return nodelist.render(context) ^^^^^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/template/base.py", line 1000, in render return SafeString("".join([node.render_annotated(context) for node in self])) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/template/base.py", line 1000, in <listcomp> return SafeString("".join([node.render_annotated(context) for node in self])) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/template/base.py", line 961, in render_annotated return self.render(context) ^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/template/defaulttags.py", line 242, in render nodelist.append(node.render_annotated(context)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/template/base.py", line 961, in render_annotated return self.render(context) ^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/template/library.py", line 237, in render output = self.func(*resolved_args, **resolved_kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/contrib/admin/templatetags/admin_list.py", line 505, in admin_list_filter "choices": list(spec.choices(cl)), ^^^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/contrib/admin/filters.py", line 150, in choices facet_counts = self.get_facet_queryset(changelist) if add_facets else None ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/contrib/admin/filters.py", line 87, in get_facet_queryset return filtered_qs.aggregate( File "/django_bug_report/.venv/lib/python3.11/site-packages/django/db/models/query.py", line 604, in aggregate return self.query.chain().get_aggregation(self.db, kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/db/models/sql/query.py", line 615, in get_aggregation result = compiler.execute_sql(SINGLE) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/db/models/sql/compiler.py", line 1562, in execute_sql cursor.execute(sql, params) ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/db/backends/utils.py", line 122, in execute return super().execute(sql, params) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/db/backends/utils.py", line 79, in execute return self._execute_with_wrappers( File "/django_bug_report/.venv/lib/python3.11/site-packages/django/db/backends/utils.py", line 92, in _execute_with_wrappers return executor(sql, params, many, context) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/db/backends/utils.py", line 100, in _execute with self.db.wrap_database_errors: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/db/utils.py", line 91, in __exit__ raise dj_exc_value.with_traceback(traceback) from exc_value ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/db/backends/utils.py", line 105, in _execute return self.cursor.execute(sql, params) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/django_bug_report/.venv/lib/python3.11/site-packages/django/db/backends/sqlite3/base.py", line 329, in execute return super().execute(query, params) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Exception Type: OperationalError at /admin/demo/customer/ Exception Value: no such column: demo_rentalitem.id
Based on the image I attached above, I'm wondering if this behavior is as expected? If so, instead of using SimpleListFilter, it used RelatedFieldListFilter.
follow-up: 6 comment:5 by , 9 months ago
Hey, thanks a lot for tackling this! You're right, for this reproducer a RelatedFieldListFilter would be the way to go.
For the reproducer we stripped some business logic where we are annotating and filtering on the queryset, so I think we have to go with subclassing a SimpleListFilter here.
comment:6 by , 9 months ago
Replying to Thomas Feldmann:
Hey, thanks a lot for tackling this! You're right, for this reproducer a RelatedFieldListFilter would be the way to go.
For the reproducer we stripped some business logic where we are annotating and filtering on the queryset, so I think we have to go with subclassing a SimpleListFilter here.
ah, I understand. After I look at the reproducer, this reproducer intended to make filter categories like "ACTIVE" and "-" (not defined, data that are not in "ACTIVE"), right?
comment:7 by , 9 months ago
Yes, kind of. The full filter is something like this:
class RentalItemsFilter(admin.SimpleListFilter): title = "State" parameter_name = "rental_items_state" def lookups(self, request, model_admin): return [ ("ACTIVE", "Active"), ("CANCELLED", "Cancelled"), ] def queryset(self, request, queryset): if not self.value(): return queryset queryset = queryset.annotate( active_rental_item_count=Count( "contracts__rental_items", filter=Q(contracts__rental_items__cancel_date__isnull=True), distinct=True, ), cancelled_rental_item_count=Count( "contracts__rental_items", filter=Q(contracts__rental_items__cancel_date__isnull=False), distinct=True, ), ) match self.value(): case "ACTIVE": return queryset.filter( active_rental_item_count__gt=0, cancelled_rental_item_count=0 ) case "CANCELLED": return queryset.filter( active_rental_item_count=0, cancelled_rental_item_count__gt=0 ) case _: raise ValueError(f"Unknown value {self.value()}")
comment:8 by , 9 months ago
Has patch: | set |
---|
Here is the PR: https://github.com/django/django/pull/17880
comment:9 by , 9 months ago
Patch needs improvement: | set |
---|
comment:10 by , 9 months ago
Patch needs improvement: | unset |
---|---|
Triage Stage: | Accepted → Ready for checkin |
Small reproducer