﻿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
26676	Prefetch with to_attr shouldn't store its result in the referenced relation cache	Arnaud B	nobody	"When using prefetch_related with a Prefetch whose to_attr is set, the related object returns a list instead of a queryset, making chaining queries impossible. This bug affects both version 1.9 and 1.10, but to a different extent. 

This minimal example illustrates the issue, the test fails with {{{AttributeError: 'list' object has no attribute 'filter'}}}

'''model''''
{{{
from django.db import models

class Book(models.Model):
    name = models.CharField(max_length=12)

class Page(models.Model):
    number = models.SmallIntegerField()
    text = models.TextField()
    book = models.ForeignKey(Book)
}}}

'''tests.py'''
{{{
from django.test import TestCase
from alpha_test.models import Book, Page
from django.db.models import Q, Prefetch


class SimpleCase(TestCase):

    def setUp(self):

        book = Book.objects.create(name=""Django Textbook"")
        page1 = Page.objects.create(number=1, text=""lorem"", book=book)
        page2 = Page.objects.create(number=2, text=""ipsum"", book=book)

    def test_prefech_related_with_Prefetch(self):

        queryset = Page.objects.filter(number=1)
        prefetch_no_attr = Prefetch('page_set', queryset=queryset)
        prefetch_with_attr = Prefetch('page_set', queryset=queryset,
                                      to_attr='first_page')
        
        # Selecting the book with the Prefetch without to_attr
        # it is possible to chain queries on the page_set
        book = Book.objects.prefetch_related(prefetch_no_attr).first()
        first_page = book.page_set.filter(number=1) # works fine

        # Now with Prefetch with to_attr set to some value,
        # the same query on page_set fails for 1.10 (but works for 1.9).
        book = Book.objects.prefetch_related(prefetch_with_attr).first()
        first_page = book.page_set.filter(number=1)
        # However, querying on the to_attr fails for both 1.9 and 1.10
        first_page = book.first_page.filter(text=""lorem"")
}}}"	Bug	closed	Database layer (models, ORM)	1.10	Release blocker	fixed	Prefetch prefetch_related queryset		Ready for checkin	1	0	0	0	0	0
