Changes between Version 1 and Version 3 of Ticket #31963


Ignore:
Timestamp:
Aug 29, 2020, 7:37:25 AM (4 years ago)
Author:
Mariusz Felisiak
Comment:

The described example works for me.

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #31963

    • Property Triage Stage AcceptedUnreviewed
    • Property Keywords Database ORM removed
    • Property Resolutionworksforme
    • Property Status newclosed
    • Property Summary Django: Nested OuterRef not woking to access grandparent's IDNested OuterRef not woking to access grandparent's ID.
  • Ticket #31963 – Description

    v1 v3  
    1 Trying to access FeeAccount model's id using **OuterRef** in a **nested subquery**, but **not immediate parent**. Throwing:
     1Trying to access `FeeAccount` model's id using **OuterRef** in a **nested subquery**, but **not immediate parent**. Throwing:
    22
    33`ValueError: This queryset contains a reference to an outer query and may only be used in a subquery.`
     
    55
    66{{{
    7 first_unpaid_fee_schedule_instalment = Subquery(FeeScheduleInstalment.objects \
    8         .annotate(
    9                 total_balance = Subquery(
    10                         FeeInstalmentTransaction.objects.filter(
    11                                 account_id=OuterRef(OuterRef('id')), # not working: need grand-parent's id here 
    12                                 instalment__schedule=OuterRef('id'), #working: parent's id
    13                         ) \
    14                         .values('balance') \
    15                         .annotate(total_balance=Sum('balance')) \
    16                         .values('total_balance')
    17                 )
    18         ) \
    19         .values('id')[:1]
     7first_unpaid_fee_schedule_instalment = Subquery(
     8    FeeScheduleInstalment.objects.annotate(
     9        total_balance = Subquery(
     10            FeeInstalmentTransaction.objects.filter(
     11                account_id=OuterRef(OuterRef('id')), # not working: need grand-parent's id here 
     12                instalment__schedule=OuterRef('id'), #working: parent's id
     13            ).values('balance').annotate(total_balance=Sum('balance')).values('total_balance')
     14        ),
     15    ).values('id')[:1]
    2016)
    2117
    22 
    23 fee_accounts_with_first_unpaid = FeeAccount.objects \
    24         .annotate(
    25                 first_unpaid_schedule = first_unpaid_fee_schedule_instalment,
     18fee_accounts_with_first_unpaid = FeeAccount.objects.annotate(
     19    first_unpaid_schedule = first_unpaid_fee_schedule_instalment,
    2620)
    2721}}}
Back to Top