Opened 5 years ago
Last modified 5 years ago
#31963 closed Bug
Django: Nested OuterRef not woking to access grandparent's ID — at Initial Version
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,
)
`