#31963: Django: Nested OuterRef not woking to access grandparent's ID -------------------------------------+------------------------------------- Reporter: | Owner: nobody ashish141199 | Type: Bug | Status: new Component: Database | Version: 2.2 layer (models, ORM) | Keywords: OuterRef, QuerySet, Severity: Normal | Django Filter, Annotation, Triage Stage: | Database, ORM Unreviewed | Has patch: 0 Needs documentation: 0 | Needs tests: 0 Patch needs improvement: 0 | Easy pickings: 0 UI/UX: 0 | -------------------------------------+------------------------------------- Trying to access FeeAccount model's id using **OuterRef** in a **nested subquery**, but **not immediate parent**. Throwing:
`ValueError: This queryset contains a reference to an outer query and may only be used in a subquery.` ``` first_unpaid_fee_schedule_instalment = Subquery(FeeScheduleInstalment.objects \ .annotate( total_balance = Subquery( FeeInstalmentTransaction.objects.filter( account_id=OuterRef(OuterRef('id')), # not working: need grand-parent's id here instalment__schedule=OuterRef('id'), #working: parent's id ) \ .values('balance') \ .annotate(total_balance=Sum('balance')) \ .values('total_balance') ) ) \ .values('id')[:1] ) fee_accounts_with_first_unpaid = FeeAccount.objects \ .annotate( first_unpaid_schedule = first_unpaid_fee_schedule_instalment, ) ``` -- Ticket URL: <https://code.djangoproject.com/ticket/31963> 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.b7ca9ac877ed02b052f8f995e4859395%40djangoproject.com.