FAQ
#22650: Regression from f51c1f59 with select_related + prefetch_related
-------------------------------------+-------------------------------------
                Reporter: aaugustin | Owner: nobody
                    Type: Bug | Status: new
               Component: Database | Version: master
   layer (models, ORM) | Keywords:
                Severity: Release | Has patch: 0
   blocker | Needs tests: 0
            Triage Stage: Accepted | Easy pickings: 0
     Needs documentation: 0 |
Patch needs improvement: 0 |
                   UI/UX: 0 |
-------------------------------------+-------------------------------------
  If a queryset does:

  - select_related through a reverse one-to-one
  - prefetch_related through that one-to-one then another relation

  and some objects don't have a related object through the reverse one-to-
  one, iterating the queryset raises an exception.

  This is easier to demonstrate with a test case:

  {{{#!diff
  diff --git a/tests/prefetch_related/models.py
  b/tests/prefetch_related/models.py
  index 8fec5d4..ef2eeb7 100644
  --- a/tests/prefetch_related/models.py
  +++ b/tests/prefetch_related/models.py
  @@ -66,6 +66,11 @@ class BookWithYear(Book):
           AuthorWithAge, related_name='books_with_year')


  +class Bio(models.Model):
  + author = models.OneToOneField(Author)
  + books = models.ManyToManyField(Book, blank=True)
  +
  +
   @python_2_unicode_compatible
   class Reader(models.Model):
       name = models.CharField(max_length=50)
  diff --git a/tests/prefetch_related/tests.py
  b/tests/prefetch_related/tests.py
  index 6732e45..0cdd8db 100644
  --- a/tests/prefetch_related/tests.py
  +++ b/tests/prefetch_related/tests.py
  @@ -9,7 +9,7 @@ from django.test import TestCase, override_settings
   from django.utils import six
   from django.utils.encoding import force_text

  -from .models import (Author, Book, Reader, Qualification, Teacher,
  Department,
  +from .models import (Author, Bio, Book, Reader, Qualification, Teacher,
  Department,
       TaggedItem, Bookmark, AuthorAddress, FavoriteAuthors, AuthorWithAge,
       BookWithYear, BookReview, Person, House, Room, Employee, Comment,
       LessonEntry, WordEntry, Author2)
  @@ -192,6 +192,9 @@ class PrefetchRelatedTests(TestCase):
                                        ["Amy"],
                                        ["Amy", "Belinda"]])

  + def test_reverse_one_to_one_then_m2m(self):
  +
  list(Author.objects.prefetch_related('bio__books').select_related('bio'))
  +
       def test_attribute_error(self):
           qs = Reader.objects.all().prefetch_related('books_read__xyz')
           with self.assertRaises(AttributeError) as cm:
  }}}

  This test fails with:

  {{{
  Traceback (most recent call last):
    File "/Users/myk/Documents/dev/django/tests/prefetch_related/tests.py",
  line 196, in test_reverse_one_to_one_then_m2m
  list(Author.objects.prefetch_related('bio__books').select_related('bio'))
    File "/Users/myk/Documents/dev/django/django/db/models/query.py", line
  141, in __iter__
      self._fetch_all()
    File "/Users/myk/Documents/dev/django/django/db/models/query.py", line
  965, in _fetch_all
      self._prefetch_related_objects()
    File "/Users/myk/Documents/dev/django/django/db/models/query.py", line
  608, in _prefetch_related_objects
      prefetch_related_objects(self._result_cache,
  self._prefetch_related_lookups)
    File "/Users/myk/Documents/dev/django/django/db/models/query.py", line
  1793, in prefetch_related_objects
      elif isinstance(getattr(first_obj, through_attr), list):
    File
  "/Users/myk/Documents/dev/django/django/db/models/fields/related.py", line
  420, in __get__
      self.related.get_accessor_name()
  RelatedObjectDoesNotExist: Author has no bio.
  }}}

  The regression exists in master and 1.7.x.

--
Ticket URL: <https://code.djangoproject.com/ticket/22650>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

--
You received this message because you are subscribed to the Google Groups "Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-updates+unsubscribe@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/052.4c3ef299d25c73f4c653fbe1deaa4fb5%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Django at May 18, 2014 at 8:10 am
    #22650: Regression from f51c1f59 with select_related + prefetch_related
    -------------------------------------+-------------------------------------
          Reporter: aaugustin | Owner: loic84
              Type: Bug | Status: assigned
         Component: Database layer | Version: master
       (models, ORM) | Resolution:
          Severity: Release blocker | Triage Stage: Accepted
          Keywords: | Needs documentation: 0
         Has patch: 0 | Patch needs improvement: 0
       Needs tests: 0 | UI/UX: 0
    Easy pickings: 0 |
    -------------------------------------+-------------------------------------
    Changes (by loic84):

      * owner: nobody => loic84
      * status: new => assigned


    --
    Ticket URL: <https://code.djangoproject.com/ticket/22650#comment:1>
    Django <https://code.djangoproject.com/>
    The Web framework for perfectionists with deadlines.

    --
    You received this message because you are subscribed to the Google Groups "Django updates" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to django-updates+unsubscribe@googlegroups.com.
    To post to this group, send email to django-updates@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/067.795b21b2ee44cf987c07576e8898103f%40djangoproject.com.
    For more options, visit https://groups.google.com/d/optout.
  • Django at May 18, 2014 at 4:35 pm
    #22650: Regression from f51c1f59 with select_related + prefetch_related
    -------------------------------------+-------------------------------------
          Reporter: aaugustin | Owner: loic84
              Type: Bug | Status: assigned
         Component: Database layer | Version:
       (models, ORM) | 1.7-beta-2
          Severity: Release blocker | Resolution:
          Keywords: | Triage Stage: Accepted
         Has patch: 0 | Needs documentation: 0
       Needs tests: 0 | Patch needs improvement: 0
    Easy pickings: 0 | UI/UX: 0
    -------------------------------------+-------------------------------------
    Changes (by timo):

      * version: master => 1.7-beta-2


    --
    Ticket URL: <https://code.djangoproject.com/ticket/22650#comment:2>
    Django <https://code.djangoproject.com/>
    The Web framework for perfectionists with deadlines.

    --
    You received this message because you are subscribed to the Google Groups "Django updates" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to django-updates+unsubscribe@googlegroups.com.
    To post to this group, send email to django-updates@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/067.966745032f2cf256a1a72eb7e9b95256%40djangoproject.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupdjango-updates @
categoriesdjango, python
postedMay 18, '14 at 8:09a
activeMay 18, '14 at 4:35p
posts3
users1
websitedjangoproject.com

1 user in discussion

Django: 3 posts

People

Translate

site design / logo © 2018 Grokbase