Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ValueError when calling prefetch_related for tracked ForeignKey fields #555

Merged
merged 6 commits into from
Dec 16, 2023
Merged

Fix ValueError when calling prefetch_related for tracked ForeignKey fields #555

merged 6 commits into from
Dec 16, 2023

Conversation

joeriddles
Copy link
Contributor

@joeriddles joeriddles commented Jan 26, 2023

Problem

Related issue: #433

Running prefetch_related on a column which has a tracker will be failed with this error:

ValueError: "prefetch_related_field_name" does not resolve to an item that supports prefetching - this is an invalid parameter to prefetch_related().

When you call prefetch_related on a ForeignKey field that is being tracked, the following error below was being raised.

___________________________________________________________________ ModelTrackedPrefetchRelatedFKTests.test_default ____________________________________________________________________

self = <tests.test_fields.test_field_tracker.ModelTrackedPrefetchRelatedFKTests testMethod=test_default>

    def test_default(self):
        self.tracker = self.instance.tracker
>       list(self.tracked_class.objects.prefetch_related("fk__fk"))

tests/test_fields/test_field_tracker.py:553: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
.venv/lib/python3.8/site-packages/django/db/models/query.py:376: in __len__
    self._fetch_all()
.venv/lib/python3.8/site-packages/django/db/models/query.py:1869: in _fetch_all
    self._prefetch_related_objects()
.venv/lib/python3.8/site-packages/django/db/models/query.py:1258: in _prefetch_related_objects
    prefetch_related_objects(self._result_cache, *self._prefetch_related_lookups)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

model_instances = [<ModelTrackedPrefetchRelatedFK: ModelTrackedPrefetchRelatedFK object (1)>], related_lookups = ('fk__fk',), done_queries = {}, auto_lookups = set()
followed_descriptors = set()

    def prefetch_related_objects(model_instances, *related_lookups):
        ...
    
                if level == len(through_attrs) - 1 and prefetcher is None:
                    # Last one, this *must* resolve to something that supports
                    # prefetching, otherwise there is no point adding it and the
                    # developer asking for it has made a mistake.
>                   raise ValueError(
                        "'%s' does not resolve to an item that supports "
                        "prefetching - this is an invalid parameter to "
                        "prefetch_related()." % lookup.prefetch_through
                    )
E                   ValueError: 'fk__fk' does not resolve to an item that supports prefetching - this is an invalid parameter to prefetch_related().

.venv/lib/python3.8/site-packages/django/db/models/query.py:2287: ValueError
=============================================================================== short test summary info ================================================================================
FAILED tests/test_fields/test_field_tracker.py::ModelTrackedPrefetchRelatedFKTests::test_default - ValueError: 'fk__fk' does not resolve to an item that supports prefetching - this ...
========================================================================== 1 failed, 333 deselected in 1.12s ===========================================================================

Solution

Explain the solution that has been implemented, and what has been changed.

Commandments

  • Write PEP8 compliant code.
  • Cover it with tests.
  • Update CHANGES.rst file to describe the changes, and quote according issue with GH-<issue_number>.
  • Pay attention to backward compatibility, or if it breaks it, explain why.
  • Update documentation (if relevant).

@joeriddles joeriddles changed the title For error on prefetch_related for tracked field For error on prefetch_related for tracked ForeignKey fields Jan 26, 2023
@joeriddles joeriddles changed the title For error on prefetch_related for tracked ForeignKey fields Fix error on prefetch_related for tracked ForeignKey fields Jan 26, 2023
@joeriddles joeriddles changed the title Fix error on prefetch_related for tracked ForeignKey fields Fix error when calling prefetch_related for tracked ForeignKey fields Jan 26, 2023
@joeriddles joeriddles changed the title Fix error when calling prefetch_related for tracked ForeignKey fields Fix ValueError when calling prefetch_related for tracked ForeignKey fields Jan 26, 2023
@Mogost
Copy link
Member

Mogost commented Nov 27, 2023

Actually looks good. @foarsitter Can we merge this?

Copy link

codecov bot commented Nov 27, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (11de64b) 95.08% compared to head (55cd816) 95.10%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #555      +/-   ##
==========================================
+ Coverage   95.08%   95.10%   +0.01%     
==========================================
  Files           6        6              
  Lines         794      796       +2     
==========================================
+ Hits          755      757       +2     
  Misses         39       39              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@joeriddles
Copy link
Contributor Author

How can I help get this merged? @Mogost @foarsitter

@foarsitter
Copy link
Contributor

You can join jazzband and merge it. I'm not familiar enough with this so I can't merge it myself.

@joeriddles joeriddles merged commit 74a064d into jazzband:master Dec 16, 2023
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants