Opened 4 years ago
Last modified 4 years ago
#31963 closed Bug
Django: Nested OuterRef not woking to access grandparent's ID — at Initial Version
Reported by: | Ashish Kulkarni | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 2.2 |
Severity: | Normal | Keywords: | OuterRef, QuerySet, Django Filter, Annotation |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
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
instalmentschedule=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,
)
`