summaryrefslogtreecommitdiff
path: root/tests/prefetch_related
AgeCommit message (Collapse)Author
2026-03-18Fixed #36987 -- Observed prepared argument in UUIDField.get_db_prep_value().Jacob Walls
This avoids two isinstance() calls per UUID value.
2025-10-16Refs #28586 -- Copied fetch modes to related objects.Adam Johnson
This change ensures that behavior and performance remain consistent when traversing relationships.
2025-10-16Fixed #28586 -- Added model field fetch modes.Adam Johnson
May your database queries be much reduced with minimal effort. co-authored-by: Andreas Pelme <andreas@pelme.se> co-authored-by: Simon Charette <charette.s@gmail.com> co-authored-by: Jacob Walls <jacobtylerwalls@gmail.com>
2025-09-16Fixed #36426 -- Added support for further iterables in ↵blingblin-g
prefetch_related_objects(). Thanks Sarah Boyce for the review.
2025-08-05Refs #36282 -- Fixed PrefetchRelatedMTICacheTests test ordering expectations.Mariusz Felisiak
2025-07-23Refs #36500 -- Rewrapped long docstrings and block comments via a script.django-bot
Rewrapped long docstrings and block comments to 79 characters + newline using script from https://github.com/medmunds/autofix-w505.
2025-07-22Fixed #36282 -- Used prefetched values in ForwardManyToOneDescriptor from ↵Take Weiland
indirect ancestors. When looking for cached values in ManyRelatedManager and ForwardManyToOneDescriptor walk up the whole chain of ancestors (as long as they are cached) to find the prefetched relation.
2025-06-04Fixed #36432 -- Fixed a prefetch_related crash on related target subclass ↵Simon Charette
queryset. Regression in 626d77e52a3f247358514bcf51c761283968099c. Refs #36116. Thanks Cornelis Poppema for the excellent report.
2025-03-26Fixed #34819 -- Made GenericForeignKey prefetching use matching pk ↵Clifford Gama
representations. Ensured that rel_obj_attr and instance_attr return matching (pk, cls) tuples in GenericForeignKey.get_prefetch_queryset(), preventing mismatches when prefetching related objects where pk and get_prep_value() differ. Using value_to_string() also makes this code compatible with composite primary keys.
2025-02-06Fixed #35677 -- Avoided non-sticky filtering of prefetched many-to-many.Simon Charette
The original queryset._next_is_sticky() call never had the intended effect as no further filtering was applied internally after the pk__in lookup making it a noop. In order to be coherent with how related filters are applied when retrieving objects from a related manager the effects of what calling _next_is_sticky() prior to applying annotations and filters to the queryset provided for prefetching are emulated by allowing the reuse of all pre-existing JOINs. Thanks David Glenck and Thiago Bellini Ribeiro for the detailed reports and tests.
2025-02-03Fixed #36135 -- Fixed reverse GenericRelation prefetching.Simon Charette
The get_(local|foreign)_related_value methods of GenericRelation must be reversed because it defines (from|to)_fields and associated related_fields in the reversed order as it's effectively a reverse GenericForeignKey itself. The related value methods must also account for the fact that referenced primary key values might be stored as a string on the model defining the GenericForeignKey but as integer on the model defining the GenericRelation. This is achieved by calling the to_python method of the involved content type in get_foreign_related_value just like GenericRelatedObjectManager does. Lastly reverse many-to-one manager's prefetch_related_querysets should use set_cached_value instead of direct attribute assignment as direct assignment might are disallowed on ReverseManyToOneDescriptor descriptors. This is likely something that was missed in f5233dc (refs #32511) when the is_cached guard was added. Thanks 1xinghuan for the report.
2025-01-15Refs #33651 -- Removed Prefetch.get_current_queryset() and ↵Sarah Boyce
get_prefetch_queryset() per deprecation timeline.
2024-08-28Fixed #35666 -- Documented stacklevel usage and testing, and adjusted test ↵Simon Charette
suite accordingly. Over the years we've had multiple instances of hit and misses when emitting warnings: either setting the wrong stacklevel or not setting it at all. This work adds assertions for the existing warnings that were declaring the correct stacklevel, but were lacking tests for it.
2024-08-13Fixed #35665 -- Fixed a crash when passing an empty order_by to Window.Simon Charette
This also caused un-ordered sliced prefetches to crash as they rely on Window. Regression in e16d0c176e9b89628cdec5e58c418378c4a2436a that made OrderByList piggy-back ExpressionList without porting the empty handling that the latter provided. Supporting explicit empty ordering on Window functions and slicing is arguably a foot-gun design due to how backends will return undeterministic results but this is a problem that requires a larger discussion. Refs #35064. Thanks Andrew Backer for the report and Mariusz for the review.
2024-03-20Fixed #35309 -- Made prefetch clear ordering for single-valued relationships.Laurent Lyaudet
2024-01-26Applied Black's 2024 stable style.Mariusz Felisiak
https://github.com/psf/black/releases/tag/24.1.0
2023-10-06Fixed #34889 -- Fixed get_prefetch_queryset() fallback in prefetch_one_level().Mariusz Felisiak
Thanks Matt Westcott for the report. Regression in cac94dd8aa2fb49cd2e06b5b37cf039257284bb0.
2023-09-18Fixed #33651 -- Added support for prefetching GenericForeignKey.Clément Escolano
Co-authored-by: revanthgss <revanthgss@almabase.com> Co-authored-by: Mariusz Felisiak <felisiak.mariusz@gmail.com>
2023-09-07Fixed #34791 -- Fixed incorrect Prefetch()'s cache for singly related objects.Maxime Toussaint
Changed the cache name used for singly related objects to be the to_attr parameter passed to a Prefetch object. This fixes issues with checking if values have already been fetched in cases where the Field already has some prefetched value, but not for the same model attr.
2023-01-17Refs #29984 -- Made QuerySet.iterator() without chunk_size raise ValueError ↵Mariusz Felisiak
after prefetch_related(). Per deprecation timeline.
2022-12-21Removed unnecessary commas in tests.Mariusz Felisiak
2022-09-30Fixed #33984 -- Reverted "Fixed #32980 -- Made models cache related managers."Mariusz Felisiak
This reverts 4f8c7fd9d91b35e2c2922de4bb50c8c8066cbbc6 and adds two regression tests: - test_related_manager_refresh(), and - test_create_copy_with_m2m(). Thanks joeli for the report.
2022-09-06Refs #26780 -- Made prefetch_related() don't use window expressions fo ↵Mariusz Felisiak
sliced queries if not supported.
2022-08-15Fixed #26780 -- Added prefetch_related() support for sliced queries.Simon Charette
This was made possible by window function filtering support added in f387d024fc75569d2a4a338bfda76cc2f328f627.
2022-04-14Relaxed some query ordering assertions in various tests.Mariusz Felisiak
It accounts for differences seen on MySQL with MyISAM storage engine.
2022-02-22Removed redundant QuerySet.all() calls in docs and tests.Nick Pope
Most QuerySet methods are mapped onto the Manager and, in general, it isn't necessary to call .all() on the manager.
2022-02-07Refs #33476 -- Refactored code to strictly match 88 characters line length.Mariusz Felisiak
2022-02-07Refs #33476 -- Reformatted code with Black.django-bot
2022-02-03Refs #33476 -- Refactored problematic code before reformatting by Black.Mariusz Felisiak
In these cases Black produces unexpected results, e.g. def make_random_password( self, length=10, allowed_chars='abcdefghjkmnpqrstuvwxyz' 'ABCDEFGHJKLMNPQRSTUVWXYZ' '23456789', ): or cursor.execute(""" SELECT ... """, [table name], )
2022-01-25Fixed #29984 -- Added QuerySet.iterator() support for prefetching related ↵Jacob Walls
objects. Co-authored-by: Raphael Kimmig <raphael.kimmig@ampad.de> Co-authored-by: Simon Charette <charette.s@gmail.com>
2022-01-05Fixed #32511 -- Corrected handling prefetched nested reverse relationships.Jamie Matthews
When prefetching a set of child objects related to a set of parent objects, we usually want to populate the relationship back from the child to the parent to avoid a query when accessing that relationship attribute. However, there's an edge case where the child queryset itself specifies a prefetch back to the parent. In that case, we want to use the prefetched relationship rather than populating the reverse relationship from the parent.
2021-11-08Fixed #32980 -- Made models cache related managers.Keryn Knight
2021-10-14Fixed #33008 -- Fixed prefetch_related() for deleted GenericForeignKeys.Martin Svoboda
Thanks Simon Charette for the implementation idea.
2021-06-04Fixed #32812 -- Restored immutability of named values from ↵Takayuki Hirayama
QuerySet.values_list(). Regression in 981a072dd4dec586f8fc606712ed9a2ef116eeee. Thanks pirelle for the report.
2020-12-16Fixed #32089 -- Fixed prefetch_related_objects() when some objects are ↵Alexey Nigin
already fetched. Thanks Dennis Kliban for the report and Adam Johnson for the initial patch. Co-authored-by: Adam Johnson <me@adamj.eu>
2020-11-06Fixed #31235 -- Made assertQuerysetEqual() compare querysets directly.Hasan Ramezani
This also replaces assertQuerysetEqual() to assertSequenceEqual()/assertCountEqual() where appropriate. Co-authored-by: Peter Inglesby <peter.inglesby@gmail.com> Co-authored-by: Mariusz Felisiak <felisiak.mariusz@gmail.com>
2020-10-06Refs #20577 -- Deferred filtering of prefetched related querysets by reverse ↵Bob Renwick
m2o relation.
2020-05-15Refs #31395 -- Relied on setUpTestData() test data isolation in various tests.Simon Charette
2020-04-30Removed unused __str__() methods in tests models.Mariusz Felisiak
Follow up to 6461583b6cc257d25880ef9a9fd7e2125ac53ce1.
2020-02-04Simplified imports from django.db and django.contrib.gis.db.Nick Pope
2019-12-09Refs #31044 -- Fixed error message when using Prefetch with a values_list() ↵Mariusz Felisiak
queryset.
2019-12-09Fixed #31044 -- Errored nicely when using Prefetch with a raw() queryset.Hasan Ramezani
2019-10-21Fixed #20577 -- Deferred filtering of prefetched related querysets.Alex Aktsipetrov
Added internal interface to QuerySet that allows to defer next filter call till .query is accessed. Used it to optimize prefetch_related(). Thanks Simon Charette for the review.
2019-10-01Fixed #30651 -- Made __eq__() methods return NotImplemented for not ↵ElizabethU
implemented comparisons. Changed __eq__ to return NotImplemented instead of False if compared to an object of the same type, as is recommended by the Python data model reference. Now these models can be compared to ANY (or other objects with __eq__ overwritten) without returning False automatically.
2019-10-01Refs #30651 -- Added tests for Prefetch.__eq__().Mariusz Felisiak
2019-08-26Fixed typo in tests/prefetch_related/tests.py comment.Tim Gates
2019-08-23Fixed typos in test names and a comment.Min ho Kim
2019-05-16Fixed #30482 -- Prevented unnecessary evaluation of lookup.queryset in ↵Iain Watts
prefetch_related_objects().
2019-04-16Fixed #30368 -- Fixed prefetch_related() for GenericForeignKey when PK is ↵Vinny Do
also a FK.
2019-04-14Fixed #30343 -- Fixed prefetch_related() for GenericForeignKey when PK of ↵Mariusz Felisiak
related field is UUIDField.