← Back to Pull requests

Answer QUERY on list views without a per-view query() handler (QueryMixin + patch()) #12

Closed
unknown closed this pull request without merging on Nov 02, 2026 20:25 UTC
View on GitHub
unknown unknown commented on Nov 02, 2026 20:06 UTC
Author

What

This removes the per-viewset query() boilerplate. Reviewer feedback on the blog post about the package suggested a QueryMixin, and pointed out that because the package already patches DRF, patch() could add query() itself and make the mixin unnecessary. This PR does both, one for each of the package's two setup paths.

  • QueryMixin (drf_query_method.mixins, also exported as drf_query_method.QueryMixin) delegates query() to list(). It's for the opt-in path, for example class NoteViewSet(QueryMixin, ModelViewSet). It works with any view that has list(), including a hand-written ViewSet.list.

  • patch() adds the same handler to DRF's ListModelMixin, and only to that. So every generic list view and list-capable viewset gets it, while views without list (RetrieveAPIView, retrieve-only viewsets) don't. Putting query on ViewSetMixin instead would have made the router build a list route for viewsets that have no list.

  • - Routers still map QUERY on the list route only, and detail routes return 405 with QUERY left out of Allow.
    - A query() that a view defines itself wins through the MRO, so patch() never overrides it.
    - On a viewset whose query() is the delegating one, the routers send QUERY straight to the list action. self.action is then "list", so a get_serializer_class(), get_queryset() or get_permissions() that branches on the action treats QUERY like GET and can't fall through to a detail serializer. A viewset with its own query() keeps the query action. Seer flagged this on the PR.
    - An @action named query keeps its own route, as before.
    - A view can opt out by leaving query out of http_method_names. That needs no new API.
    - This part also applies once DRF supports QUERY natively, because DRF's own support (encode/django-rest-framework#10091) only routes QUERY to views that define query(). Otherwise upgrading DRF would silently turn these routes into 405s. The usage docs cover adding handlers before uninstalling.
  • drf-spectacular guard. drf-spectacular 0.30 enumerates methods itself and crashes with KeyError: 'query' on any view with a query() handler. Once patch() gives every list view that handler, every drf-spectacular user's schema would break. When drf-spectacular is installed, patch() now leaves QUERY out of its schema, the same way it already does for DRF's own generator. (drf-yasg uses DRF's get_allowed_methods, which the package already patches.) drf-spectacular is added to the test dependency group and to the CI and just test-against installs.

  • The opt-in drf_query_method.viewsets classes don't include QueryMixin, so they stay explicit.
  • Behaviour change for existing patch() users: list endpoints that used to return 405 for QUERY now answer it, with the same permissions and filters as GET. This is recorded under Changed in the changelog.

    No version bump. There's no docs/adr/ in this repo, so there's no ADR. This PR and #11 both edit the README, the usage docs and CHANGELOG/unreleased.md, so whichever merges second will need a small rebase.

    AI Provenance

  • Tool: Claude Code

  • Model: Claude Opus 5.5

  • Prompt/thread: Implement the reviewer's QueryMixin suggestion, and have patch() add the equivalent handler where it's safe without overriding a view's own query().
  • Checklist

  • Addresses exactly one issue or feature

  • New or changed behavior has test coverage

  • Diff contains only changes for this task
  • just check passes (ruff format, ruff check, ty, 71 tests), and just testall passes on Python 3.10/Django 4.2/DRF 3.16, 3.13/5.2/3.17 and 3.14/6.1/3.18. just docs-build is clean. Removing the ListModelMixin patch makes two tests fail, removing the drf-spectacular guard makes one fail, and removing the list action mapping makes three fail.

    Diff: 487 lines (+365 / -122) across 6 files Reviewer: BenA-SA Review duration: 19m
    All Vouch review confidence checks passed
    Residual risk 0.19 = Change Risk (0.47) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.47 · 487 lines diff across 6 files
    0.47
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.370
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #12"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 47.4% (MEDIUM) · Higher = larger blast radius
    47.4%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 18.7% · Safe to merge
    18.7%