﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
27744	Complex prefetch_related broken (from 1.9 to 1.10)	Pascal Briet	nobody	"Was working on : 1.9.10
Is not working on : 1.10.5

{{{
#!div style=""font-size: 80%""
  {{{#!python
from offer.models import Price, PriceGrid
from django.db.models import Prefetch


price_queryset = Price.objects.prefetch_related('calculation_rules__rule')

pricegrids = PriceGrid.objects.all()
pricegrids = pricegrids.prefetch_related(Prefetch('prices', queryset=price_queryset))

for pg in pricegrids: print(pg.pk)
  }}}
}}}

`AttributeError: Cannot find 'rule' on RelatedManager object, 'prices__calculation_rules__rule' is an invalid parameter to prefetch_related()`

But this still works :


{{{
#!div style=""font-size: 80%""
  {{{#!python

pgs = PriceGrid.objects.prefetch_related('prices__calculation_rules__rule').all()

for pg in pgs:
    for price in pg.prices.all():
        for dr in price.calculation_rules.all():
             print(dr.rule.pk)

  }}}
}}}

Models (extracts) :

{{{
#!div style=""font-size: 80%""
  {{{#!python
class Price(models.Model):
    grid                = models.ForeignKey(PriceGrid, related_name=""prices"")

class PriceCalculationRule(models.Model):
    price  = models.ForeignKey(Price, on_delete=models.CASCADE, related_name=""calculation_rules"")
    rule   = models.ForeignKey(Rule, on_delete=models.PROTECT, verbose_name=""Condition"")
   
  }}}
}}}"	Bug	closed	Database layer (models, ORM)	1.10	Normal	duplicate	prefetch		Unreviewed	0	0	0	0	0	0
