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
QUERYrequest toSearchViewraisesTypeError: 'str' object is not callable(HTTP 500). On 6.1 it returns 405. - An
OPTIONSrequest (or a 405 response) listsQUERYin theAllowheader. - With
query = Noneand anasync def get(),view_is_asyncraisesImproperlyConfigured: ... HTTP handlers must either be all sync or all async.whenas_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_propertynamedquery(for exampledef query(self)returning a queryset) now runs forQUERYrequests and fails withTypeError(500). SearchView.as_view(query="x")now raisesTypeError: 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 , 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 definehttp_method_nameson the view, no?