Repository navigation
Conversation
Django 6.2 adds QUERY to View.http_method_names, which already fails two tests in tests/test_decorators.py against Django main. Support it properly on Django 6.2 and later: - Add QUERY to SAFE_METHODS and to the perms_map of DjangoModelPermissions and DjangoObjectPermissions. - Add MethodMapper.query(), and .query() to APIRequestFactory and APIClient. - Route QUERY to a viewset's query() on the list route. A viewset only answers QUERY where the route maps it, and an existing @action named query keeps its own route. - Add Request.filter_params, which merges a QUERY request's parsed body over its URL query parameters, and read it from SearchFilter and OrderingFilter. Pagination parameters stay in the URL. - Exclude QUERY from the deprecated OpenAPI generator, since OpenAPI 3.0 has no query operation.
- patch() applies encode/django-rest-framework#10091 to DRF in place: routers, viewsets, SearchFilter, OrderingFilter, SAFE_METHODS and the Django permission maps, request.filter_params, MethodMapper.query(), the test factory and client, and the deprecated schema generator. When django-filter is installed, DjangoFilterBackend reads the body too. It leaves DRF alone once DRF supports QUERY itself, and unpatch() reverts it for tests. - Opt-in classes with the same names as DRF's and django-filter's, for projects that would rather change imports than patch. - add_query_method() lets Django before 6.2 dispatch QUERY. - django-filter receives body values as strings, so JSON numbers and booleans don't reach widgets that split on commas. Tested across Django 4.2-6.1 with DRF 3.16-3.18, Django main with DRF main, and DRF with native QUERY support.
|
Repro on head from rest_framework import viewsets
from rest_framework.response import Response
from rest_framework.routers import SimpleRouter
class VS(viewsets.ViewSet):
def query(self, request):
return Response({"params": dict(request.filter_params)})
router = SimpleRouter()
router.register("vs", VS, basename="vs")
urlpatterns = router.urls
Fix matches the existing # QUERY method (RFC 10008) is not implemented by Django before 6.2
if 'query' not in View.http_method_names:
View.http_method_names = View.http_method_names + ['query']With that added and nothing else changed, the same request returns |
APIView.dispatch() only routes methods listed in View.http_method_names, which Django adds `query` to from 6.2. On earlier versions viewset and router `query()` handlers returned 405, while `@api_view(["QUERY"])` worked because it builds its own list. Add `query` in compat, as is already done for PATCH, so QUERY works on every supported Django version. The `requires_query_method` test marker is no longer needed, so the QUERY tests now run on every supported Django version. Co-authored-by: Ege <68082325+ege-arhan@users.noreply.github.com>
QUERY is a safe method, so it needs no CSRF token. Django's CSRF check exempts it from 6.2 onwards, but earlier versions only exempt GET, HEAD, OPTIONS and TRACE, so session-authenticated QUERY requests without a token were rejected with 403. Skip the check for QUERY in SessionAuthentication.enforce_csrf(), matching Django 6.2 on every supported version.
|
Thanks @ege-arhan, good catch. I reproduced the 405 on 6.1 and confirmed that Your change also exposed a second gap. On Django before 6.2, One side effect: the existing caveat about a viewset helper or attribute named |
browniebroke
left a comment
There was a problem hiding this comment.
Haven't looked in detail yet, but should this be available only for Django 6.1? Should we build this on top of the APIs from django/django#21855?
I'm a bit surprised to the see that the tests pass on all versions, including the ones running against older Django versions.
| def filter_params(self): | ||
| """ | ||
| The parameters that filter backends should read. | ||
|
|
||
| For most methods this is `request.query_params`. A `QUERY` request | ||
| (RFC 10008) carries its query in the request body, so the parsed body | ||
| is merged over the URL query parameters, with the body taking | ||
| precedence for any key present in both. | ||
| """ | ||
| if self.method != 'QUERY': | ||
| return self.query_params | ||
|
|
||
| body = self.data | ||
| if not isinstance(body, Mapping): | ||
| raise exceptions.ParseError( | ||
| 'QUERY request body must be an object of parameters, ' | ||
| 'not %s.' % type(body).__name__ | ||
| ) | ||
|
|
||
| if not isinstance(body, MultiValueDict): | ||
| body = MultiValueDict({ | ||
| key: value if isinstance(value, list) else [value] | ||
| for key, value in body.items() | ||
| }) | ||
|
|
||
| params = self.query_params.copy() | ||
| for key, values in body.lists(): | ||
| params.setlist(key, values) | ||
| return params |
There was a problem hiding this comment.
That feels like the main thing we should pay attention to...
| # QUERY method (RFC 10008) is not implemented by Django before 6.2 | ||
| if 'query' not in View.http_method_names: | ||
| View.http_method_names = View.http_method_names + ['query'] |
There was a problem hiding this comment.
Right, I see... I'm not a big fan of reaching into Django like that.
There was a problem hiding this comment.
I worked on QUERY in Django and I see why you don't like it but it would also feel 'weird' to say: DRF 3.19 has QUERY but only if your Django is > 6.2; especially because DRF typically supports all current django's. But maybe it's unavoidable. It would be similarly weird if DRF would only start supporting QUERY when django 6.2 is the 'oldest supported' version....
There was a problem hiding this comment.
I'm happy to implement either route - I thought it made sense to try and support the new Query method as much as possible retrospectively in case there were projects that would benefit from it that would have a difficult time upgrading to Django 6.2 + DRF 3.19, hence this approach, however I appreciate your point of view too in preferring not to reach into Django. Obviously we'd need to reach into Django to support Query retrospectively, so it seems to me that it's one or the other, but I'm happy to be guided on which.
There was a problem hiding this comment.
I worked on QUERY in Django
Thanks for jumping in!
it would also feel 'weird' to say: DRF 3.19 has QUERY but only if your Django is > 6.2
I don't know... Consider the opposite: if you install Django 6.0 and DRF 3.19, you would have support for the QUERY method only in DRF view, not in plain Django views, which feels a bit weird to me too.
It would be similarly weird if DRF would only start supporting QUERY when django 6.2 is the 'oldest supported' version....
Agree on this, that's definitely not an option, as it would only be in a very long time :)
TBH, I haven't made up my mind yet, it's just something I noted while browsing the diff.
I'm slightly concerned about the unintended side effects of doing this monkey patching, as I can imagine it might trip up other 3rd party Django packages. I can easily see someone else checking this for feature detecting QUERY support in Django.
On the other hand, implementing the QUERY method in DRF only if Django supports it might be itself technically challenging, that would probably add a lot of if branches to this patch.
I probably need to get up to speed with the Django patch to review this properly.
I'm happy to implement either route
Thanks for the offer. I would recommend to NOT waste time/tokens on that just yet :)
Refs discussion #10024.
Django 6.2 adds the
QUERYmethod from RFC 10008 (django/django#21855): safe and idempotent likeGET, but with a request body. It adds"query"toView.http_method_names, treatsQUERYas CSRF-safe, and addsRequestFactory.query().That already breaks DRF against Django main: two tests in
tests/test_decorators.pymap every name inhttp_method_namesontohttp.HTTPMethod, which has noQUERYmember. This PR fixes those and adds support forQUERYon every supported Django version. On Django 6.1 and earlier it addsQUERYthe same waycompat.pyalready addsPATCH, and matches Django 6.2's CSRF handling, soQUERYbehaves the same on all of them.Size
rest_framework/)tests/)docs/)The change to how DRF behaves is 85 lines. 32 of those add
Request.filter_params. Most of the diff is tests.Changes
QUERYis added toSAFE_METHODS, matching the RFC and Django's CSRF handling. It is also added with no required permissions ('QUERY': []) to theperms_mapof bothDjangoModelPermissionsandDjangoObjectPermissions. If a project overridesperms_mapand doesn't listQUERY, those classes refuse it with 405.SimpleRoutermapsQUERYto a viewset'squery()on the list route only. Viewsets that don't definequery()are unaffected. A viewset only answersQUERYon routes that map it, so detail routes return 405 and leave it out ofAllow. An existing@actionnamedquerykeeps its own route and isn't bound to theQUERYmethod.Request.filter_params: for aQUERYrequest, this merges the parsed body over the URL query parameters, with the body winning when a key appears in both. For every other method it is simplyquery_params.SearchFilterandOrderingFilterread from it, so aQUERYlist view can carry its filters in the body. Pagination parameters still come from the URL. If the body isn't a JSON object or form, filtering returns 400.compat.pyaddsquerytoView.http_method_names, as it already does forpatch. Without it, viewset and routerquery()handlers returned 405 while@api_view(["QUERY"])worked. Thanks to @ege-arhan, who spotted this and suggested the fix; they're credited as co-author on that commit.SessionAuthenticationdoesn't require a CSRF token forQUERY. Django 6.2 exempts it itself; on earlier versions session-authenticatedQUERYrequests without a token were rejected with 403.APIRequestFactory.query()andAPIClient.query()acceptformatandcontent_type, likepost().MethodMappergains.query().QUERY, because OpenAPI 3.0 has no query operation.Things worth reviewing
SAFE_METHODS. This is the line with the most security impact.IsAuthenticatedOrReadOnlyandDjangoModelPermissionsOrAnonReadOnlywill now let anonymous users sendQUERYto views that handle it. AQUERYhandler must therefore never write, which is what the RFC requires. Django makes the same call for CSRF.filter_paramsis the only new public API. Without it, the built-in filters ignore the body of aQUERYrequest. It also gives third-party filter backends one place to readQUERYparameters from, and I plan to propose using it in django-filter and django-rest-framework-datatables, so I'd prefer to keep it in this PR.query. If a viewset has a non-handler attribute or helper method calledquery, Django 6.2 already treats it as aQUERYhandler. WithAllowAnythat crashes upstream on every route. With this PR it is limited to the list route, but combined withDjangoModelPermissionsthat route now returns 500 rather than 405. Becausecompat.pynow addsQUERYon earlier Django versions too, this applies there as well, including to plain Django views with aqueryattribute, as it will on 6.2.Not included
query(), so I'll raise that with drf-spectacular separately.Testing
Each new test was checked by reverting the line of code it covers and confirming that it fails. The
QUERYtests run on every Django version.