#30739: OuterRef does not work with exclude() or ~Q() -------------------------------------+------------------------------------- Reporter: Oskar | Owner: nobody Persson | Type: Bug | Status: new Component: Database | Version: 2.2 layer (models, ORM) | Severity: Normal | Keywords: Triage Stage: | Has patch: 0 Unreviewed | Needs documentation: 0 | Needs tests: 0 Patch needs improvement: 0 | Easy pickings: 0 UI/UX: 0 | -------------------------------------+------------------------------------- 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 }}} -- Ticket URL: <https://code.djangoproject.com/ticket/30739> Django <https://code.djangoproject.com/> The Web framework for perfectionists with deadlines. -- You received this message because you are subscribed to the Google Groups "Django updates" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-updates+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/055.67a4f7e41598bf9f1bd2e267204d5276%40djangoproject.com.