Skip to content

Add ORM query autocomplete inside .filter() / .exclude() / .annotate() #3

Closed

Description

@FROWNINGdev

This is the moat feature identified in competitor analysis: no shipped Django extension does ORM query autocomplete well.

Goal: when the user types inside Post.objects.filter(...), complete field names based on the parsed model index, and resolve lookups like author__name through FK edges.

Design sketch:

  1. Detect .filter( / .exclude( / .annotate( call inside a Python file via lightweight regex or textDocument context.
  2. Resolve the QuerySet's base model from the assignment chain (best-effort).
  3. Suggest field names + __lookup traversal from the parsed WorkspaceIndex.
  4. Warn on typos with a diagnostic.

Prior art: the archived django-intellisense extension had a partial implementation worth studying.

Bigger feature — likely v0.5+. Design discussion welcome on Discussions first.

Activity

  1. FROWNINGdev commented on Aug 20, 2026

    @FROWNINGdev
    OwnerAuthor

    Shipped in v0.13.0, live on the Marketplace and Open VSX. Open since day two of the project.

    Against the design sketch above

    1. Detect the call — done, and stricter than "lightweight regex". The innermost unclosed ( is found by walking the line with a quote-aware scanner, so label = 'filter(' + x does not open a completion context. Scope is filter, exclude, get, get_or_create, update_or_create, annotate, aggregate, order_by, values, values_list, only, defer, distinct, create, update. A Q(...) / F(...) / Count(...) wrapper is stepped out of, so filter(Q(auth completes auth — not Q.

    2. Resolve the base model — best-effort, and deliberately narrow. The literal <Model>.objects.<method>( shape resolves, including down a chain: Post.objects.filter(published=True).exclude(ti resolves to Post. Anything else returns nothing rather than a guess. An empty list costs the user nothing; a list of fields from the wrong model costs them a debugging session. Assignment-chain inference (qs = Post.objects.all() on an earlier line, then qs.filter() is the obvious next increment and is not in this release.

    3. Fields plus __ traversal — done. author__ walks the FK and offers the related model's fields; two edges compose (parent__author__name); ForeignKey('self') resolves back to its own model; pk is offered everywhere even though it is not a declared field. Lookups are typed rather than a flat list — title__ offers icontains but not gt, views__ offers gte and range but not icontains, created__ adds year/month/hour/date, and BooleanField stays at exact/in/isnull.

    4. Warn on typos with a diagnostic — not in this release. A completion that is silent when unsure is safe; a diagnostic that is wrong when unsure is an error squiggle on correct code. That wants the model resolution from (2) to be stronger first. Worth its own issue rather than holding this one open.

    Two details that were invisible until they were wrong, both now under test: _ is a trigger character alongside ( and ,, because a lookup path never leaves a word and VS Code would not re-query after author__ on its own; and the replaced range is the whole lookup path rather than VS Code's default word range, which stops at the last __ and would land author__name as author__author__name.

    The decision is completionsAt() in src/ormCompletions.ts — a pure function over one line of text and the parsed index, no vscode import, 30 tests, verified against real parser output rather than only a fixture. Suite is at 176, up from 146.

    New setting djangoOrmLens.completions.enabled (default true) for anyone who already has a Python completion source they prefer here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requesthelp wantedExtra attention is needed

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions