Opened 3 months ago

Closed 3 months ago

Last modified 3 months ago

#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 }}">&#10006; {% 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)

django_bug_report.zip (44.6 KB ) - added by Simon Alef 3 months ago.
Small reproducer
screenshot-related-list-filter.png (162.8 KB ) - added by Shafiya Adzhani 3 months ago.
Screenshot of Related List Filter on reported apps

Download all attachments as: .zip

Change History (15)

by Simon Alef, 3 months ago

Attachment: django_bug_report.zip added

Small reproducer

comment:1 by Thomas Feldmann, 3 months ago

Cc: Thomas Feldmann added

comment:2 by Mariusz Felisiak, 3 months ago

Cc: Sarah Boyce added
Severity: NormalRelease blocker
Summary: Django Admin "Show counts" Feature Triggers Exception in a Custom FilterFacet filters crash for queryset with no ID.
Triage Stage: UnreviewedAccepted

Thanks for the report. Regression in 868e2fcddae6720d5713924a785339d1665f1bb9.

comment:3 by Shafiya Adzhani, 3 months ago

Owner: changed from nobody to Shafiya Adzhani
Status: newassigned

Will try to look up and fix this.

Last edited 3 months ago by Shafiya Adzhani (previous) (diff)

by Shafiya Adzhani, 3 months ago

Screenshot of Related List Filter on reported apps

in reply to:  description comment:4 by Shafiya Adzhani, 3 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 }}">&#10006; {% 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.

comment:5 by Thomas Feldmann, 3 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.

in reply to:  5 comment:6 by Shafiya Adzhani, 3 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?

Last edited 3 months ago by Shafiya Adzhani (previous) (diff)

comment:7 by Thomas Feldmann, 3 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 Shafiya Adzhani, 3 months ago

Has patch: set

comment:9 by Sarah Boyce, 3 months ago

Patch needs improvement: set

comment:10 by Mariusz Felisiak, 3 months ago

Patch needs improvement: unset
Triage Stage: AcceptedReady for checkin

comment:11 by Mariusz Felisiak <felisiak.mariusz@…>, 3 months ago

Resolution: fixed
Status: assignedclosed

In a7382812:

Fixed #35198 -- Fixed facet filters crash on querysets with no primary key.

Thanks Simon Alef for the report.

Regression in 868e2fcddae6720d5713924a785339d1665f1bb9.

comment:12 by Mariusz Felisiak <felisiak.mariusz@…>, 3 months ago

In 80761c3b:

[5.0.x] Fixed #35198 -- Fixed facet filters crash on querysets with no primary key.

Thanks Simon Alef for the report.

Regression in 868e2fcddae6720d5713924a785339d1665f1bb9.

Backport of a738281265bba5d00711ab62d4d37923764a27eb from main

comment:13 by Natalia Bidart, 3 months ago

#35282 was a duplicate of this one.

Note: See TracTickets for help on using tickets.
Back to Top