Opened 29 minutes ago

Last modified 92 seconds ago

#37522 assigned Cleanup/optimization

Class-based views with a `query` attribute crash on QUERY requests since "query" was added to View.http_method_names

Reported by: Mariano Ignacio Baragiola Owned by: Mariano Ignacio Baragiola
Component: Documentation Version: dev
Severity: Release blocker Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

5c13b5a198 (#37232) added "query" to View.http_method_names. View treats any attribute whose name is in that list as a handler (dispatch(), view_is_async, _allowed_methods()), and query is a common name for an attribute on a search view. Such views now break:

from django.http import HttpResponse
from django.views import View


class SearchView(View):
    query = ""

    def get(self, request):
        return HttpResponse(self.query)
  • A QUERY request to SearchView raises TypeError: 'str' object is not callable (HTTP 500). On 6.1 it returns 405.
  • An OPTIONS request (or a 405 response) lists QUERY in the Allow header.
  • With query = None and an async def get(), view_is_async raises ImproperlyConfigured: ... HTTP handlers must either be all sync or all async. when as_view() runs, so the URLconf fails to import and the project does not start.

Two more cases follow from the same change but can't be fixed without undoing it:

  • A helper method or @cached_property named query (for example def query(self) returning a queryset) now runs for QUERY requests and fails with TypeError (500).
  • SearchView.as_view(query="x") now raises TypeError: The method name query is not accepted as a keyword argument to SearchView().

The 6.2 release notes list QUERY support only under "What's new", not under backwards incompatible changes.

Versions affected: main only (not in stable/6.1.x). Regression in 5c13b5a198.

Suggested fix: treat a method name as handled only when the attribute is callable, in dispatch(), view_is_async, and _allowed_methods(). Views with a non-callable query attribute then behave as in 6.1. Add a note to the 6.2 backwards incompatible changes for the two cases above (a query() method written for another purpose, and as_view(query=...)). Patch: PR to follow.

AI disclosure: I found and reproduced this with Claude Code (Anthropic).

Change History (1)

comment:1 by Jacob Walls, 92 seconds ago

Component: Generic views → Documentation
Triage Stage: Unreviewed → Accepted
Type: Bug → Cleanup/optimization

I think we should fix this with a backwards-incompatible change notice. Happy to take an amendment in the release notes. But this is a consequence of the intentional change to http_method_names, which is already doc'd. The workaround is to define http_method_names on the view, no?

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