Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#31044 closed Cleanup/optimization (fixed)

Prefetch() assumes that a given queryset has an _iterable_class.

Reported by: Keryn Knight Owned by: Hasan Ramezani
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Problem reported on IRC, where the exception was essentially obscuring from the user why they can't proceed...

The __init__ method for Prefetch tests whether the ._iterable_class is a subclass of ModelIterable in an effort to provide a runtime exception about trying to use values() (which is itself an oddly specific error message, because doesn't values_list() also return a non-ModelIterable?)

But there's at least one ostensible queryset type which one might try to use, which doesn't implement the same private API behind it's __iter__: RawQuerySet (as returned by MyObject.objects.raw(...))

Attempting to use Prefetch("x", queryset=MyObject.objects.raw(...)) thus raises an AttributeError when attempting to check whether it should raise a ValueError about values() usage.

I think it should actually test for hasattr(queryset, "_iterable_class") and raise a TypeError or ValueError about that, before trying to proceed with testing whether a specific private attribute is the correct type.

Change History (8)

comment:1 by Mariusz Felisiak, 5 years ago

Summary: Prefetch objects assume a given queryset will have an _iterable_class, so that an exception can be raised for usage of .values()Prefetch() assumes that a given queryset has an _iterable_class.
Triage Stage: UnreviewedAccepted
Version: 2.2master

comment:2 by Claude Paroz, 5 years ago

Regression in [7ec330eeb96d0]

comment:3 by Hasan Ramezani, 5 years ago

Owner: changed from nobody to Hasan Ramezani
Status: newassigned

comment:4 by Hasan Ramezani, 5 years ago

Has patch: set
Version 0, edited 5 years ago by Hasan Ramezani (next)

comment:5 by Mariusz Felisiak, 5 years ago

Patch needs improvement: set

comment:6 by Hasan Ramezani, 5 years ago

Patch needs improvement: unset

comment:7 by Mariusz Felisiak <felisiak.mariusz@…>, 5 years ago

Resolution: fixed
Status: assignedclosed

In 4540842b:

Fixed #31044 -- Errored nicely when using Prefetch with a raw() queryset.

comment:8 by Mariusz Felisiak <felisiak.mariusz@…>, 5 years ago

In d650527:

Refs #31044 -- Fixed error message when using Prefetch with a values_list() queryset.

Note: See TracTickets for help on using tickets.
Back to Top