Django 预取嵌套 FK 不起作用

Django Prefetching Nested FKs not working

我已经为此做了一些工作,但我似乎无法在我的实例中正确地进行预取。我正在尝试 运行 嵌套 Prefetch() 从预取对象中预取项目,但该属性没有像往常一样存储。有人可以告诉我我做错了什么吗?

这是我的代码:

primary_residents = Resident.objects.filter(
        property=request.session['property'], type='primary', user__is_active=True
    ).select_related(
        'unit', 'unit__building', 'user'
    ).prefetch_related(
        Prefetch('current_balance',
                 queryset=Balance.objects.filter(is_active=True).prefetch_related(
                     Prefetch('charges',
                              queryset=Charges.objects.filter(date_entered__range=(
                                      previous_month, property.close_out_start_date_plus_one()), reversed=False
                              ).prefetch_related(
                                  'code', 'balance'),
                              to_attr='previous_charges'
                              )
                 ), to_attr='active_balance'
                 ),
    )
for p in primary_residents:
    # errors out, previous_charges is not a valid attr
    print(p.active_balance.previous_charges)

我想出了解决办法。

primary_residents = Resident.objects.filter(
        property=request.session['property'], type='primary', user__is_active=True
    ).select_related(
        'unit', 'unit__building', 'user'
    ).prefetch_related(
        Prefetch('current_balance',
                 queryset=Balance.objects.all().prefetch_related(
                     Prefetch('charges',
                              queryset=Charges.objects.filter(date_entered__range=(previous_month, property.close_out_start_date_plus_one()), has_been_reversed=False
                                                              ).prefetch_related('code', 'balance', 'charges_history'),
                              to_attr='previous_charges'
                              ),
                     Prefetch('payments',
                              queryset=Payments.objects.filter(date_entered__range=(previous_month, property.close_out_start_date_plus_one()), has_been_returned=False
                                                               ).prefetch_related('code', 'balance'),
                              to_attr='previous_payments'
                              ),
                     Prefetch('charges',
                              queryset=Charges.objects.filter(date_entered__range=(previous_month, property.close_out_start_date_plus_one()), has_been_reversed=False
                                                              ).prefetch_related('code', 'balance'),
                              to_attr='current_charges'
                              ),
                     Prefetch('payments',
                              queryset=Payments.objects.filter(date_entered__range=(previous_month, property.close_out_start_date_plus_one()), has_been_returned=False
                                                               ).prefetch_related('code', 'balance'),
                              to_attr='current_payments'
                              ),
                 )
                 )
    )
    for p in primary_residents:
            print(p.current_balance.previous_charges)