OuterRef in exclude() or ~Q() uses wrong model.
Description
The following test (added to tests/queries/test_qs_combinators) fails when trying to exclude results using OuterRef()
def test_exists_exclude(self):
# filter()
qs = Number.objects.annotate(
foo=Exists(
Item.objects.filter(tags__category_id=OuterRef('pk'))
)
).filter(foo=True)
print(qs) # works
# exclude()
qs = Number.objects.annotate(
foo =Exists(
Item.objects.exclude(tags__category_id=OuterRef('pk'))
)
).filter(foo=True)
print(qs) # crashes
# filter(~Q())
qs = Number.objects.annotate(
foo =Exists(
Item.objects.filter(~Q(tags__category_id=OuterRef('pk')))
)
).filter(foo=True)
print(qs) # crashes
It results in the following error
ValueError: This queryset contains a reference to an outer query and may only be used in a subquery
Work only inside this repository checkout. Make the code change the task
describes, keeping the diff focused — no drive-by refactors.
When you are done, leave your changes committed or in the working tree;
they are collected automatically.
Stay on this snapshot checkout (`task/ycb_django_pr11734`). Never checkout, pull, or rebase onto `main`. That branch is a README-only orphan.
Stay on this HEAD. Do not fetch another default branch. Push only on the Cursor-created `crazy-cursor/…` side branch from this HEAD.
Some past runs of this task were launched with a different prompt (the prompt template changed since, or those runs predate this benchmark's stored prompt). Each run persists the exact prompt it sent at launch — that per-launch record is the audit trail; this page shows only the current one.
Reference diff
The task PR's merged diff — the judge's comparison target.