Skip to content

Commit

Permalink
[5.0.x] Fixed #35426 -- Updated querysets to be a required argument o…
Browse files Browse the repository at this point in the history
…f GenericPrefetch.

Backport of 9a27c76 from main.
  • Loading branch information
sobolevn authored and sarahboyce committed May 4, 2024
1 parent ac9e18f commit 9b5029f
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 2 deletions.
2 changes: 1 addition & 1 deletion django/contrib/contenttypes/prefetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


class GenericPrefetch(Prefetch):
def __init__(self, lookup, querysets=None, to_attr=None):
def __init__(self, lookup, querysets, to_attr=None):
for queryset in querysets:
if queryset is not None and (
isinstance(queryset, RawQuerySet)
Expand Down
2 changes: 1 addition & 1 deletion docs/ref/contrib/contenttypes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ information.

.. versionadded:: 5.0

.. class:: GenericPrefetch(lookup, querysets=None, to_attr=None)
.. class:: GenericPrefetch(lookup, querysets, to_attr=None)

This lookup is similar to ``Prefetch()`` and it should only be used on
``GenericForeignKey``. The ``querysets`` argument accepts a list of querysets,
Expand Down
3 changes: 3 additions & 0 deletions docs/releases/5.0.5.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,6 @@ Bugfixes

* Fixed a bug in Django 5.0 that caused a migration crash when altering a
``GeneratedField`` referencing a renamed field (:ticket:`35422`).

* Fixed a bug in Django 5.0 where the ``querysets`` argument of
``GenericPrefetch`` was not required (:ticket:`35426`).
8 changes: 8 additions & 0 deletions tests/contenttypes_tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,14 @@ def test_multidb(self):


class GenericPrefetchTests(TestCase):
def test_querysets_required(self):
msg = (
"GenericPrefetch.__init__() missing 1 required "
"positional argument: 'querysets'"
)
with self.assertRaisesMessage(TypeError, msg):
GenericPrefetch("question")

def test_values_queryset(self):
msg = "Prefetch querysets cannot use raw(), values(), and values_list()."
with self.assertRaisesMessage(ValueError, msg):
Expand Down

0 comments on commit 9b5029f

Please sign in to comment.