Skip to content

Commit

Permalink
Merge pull request #68 from jpic/prefetch_related_lookups
Browse files Browse the repository at this point in the history
  • Loading branch information
clokep authored Jul 27, 2020
2 parents 52d8f85 + 40eb0b5 commit 457ff0e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
14 changes: 14 additions & 0 deletions queryset_sequence/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,20 @@ def _fetch_all(self):
if self._result_cache is None:
self._result_cache = list(self._iterable_class(self))

@property
def _prefetch_related_lookups(self):
# A hack for ModelChoiceField, which uses internal APIs from QuerySet.
# This should be fixed when https://code.djangoproject.com/ticket/29984
# is fixed.
#
# Note that this really just needs to return a truth-y value if any of
# the QuerySets are using prefetch_related, but this tries to keep the
# type sane at least.
result = ()
for qs in self._querysets:
result += qs._prefetch_related_lookups
return result

# Python magic methods.

def __len__(self):
Expand Down
8 changes: 8 additions & 0 deletions tests/test_querysetsequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from unittest.mock import patch

import django
from django import forms
from django.core.exceptions import (FieldDoesNotExist,
FieldError,
MultipleObjectsReturned,
Expand Down Expand Up @@ -354,6 +355,13 @@ def test_empty_prefetch_related(self):
"""Calling prefetch_related on an empty QuerySetSequence doesn't error."""
self.empty.prefetch_related('author')

def test_model_choice_iterator(self):
"""Ensure that ModelChoiceField works for QuerySetSequence."""
f = forms.ModelChoiceField(self.all)
self.assertEqual(list(f.choices), [
('', '---------'),
] + [(it.pk, str(it)) for it in self.all])


class TestDeferOnly(TestBase):
def test_defer(self):
Expand Down

0 comments on commit 457ff0e

Please sign in to comment.