Opened 20 months ago

Closed 20 months ago

Last modified 20 months ago

#27744 closed Bug (duplicate)

Complex prefetch_related broken (from 1.9 to 1.10)

Reported by: Pascal Briet Owned by: nobody
Component: Database layer (models, ORM) Version: 1.10
Severity: Normal Keywords: prefetch
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Was working on : 1.9.10
Is not working on : 1.10.5

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 :

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) :

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")
   

Change History (3)

comment:1 Changed 20 months ago by Simon Charette

Could you also include the definition of your PriceGrid model?

comment:2 Changed 20 months ago by Simon Charette

Resolution: duplicate
Status: newclosed

This looks like a duplicate of #27554.

comment:3 Changed 20 months ago by Pascal Briet

Thanks, and sorry for the duplicate

Note: See TracTickets for help on using tickets.
Back to Top