#24277: Passing a dict with 2 keys to a queryset filter passes erroneously.
----------------------------------------------+--------------------
Reporter: kezabelle | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Database layer (models, ORM) | Version: master
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------------------+--------------------
It's reasonably common to do this:
{{{
kws = {'first_name': 'test', 'is_superuser': False}
get_user_model().objects.filter(**kws)
}}}
when building up a set of queryset filters, at least if you don't need to
introduce ORs (via `Q()`).
However, if you forget, or accidentally don't star-expand the dictionary:
{{{
get_user_model().objects.filter(kws)
}}}
You instead get back a queryset with the wrong SQL applied, specifically,
above it yields `WHERE first_name = 'is_superuser'`, though that may be a
function of the relative stableness of dicts on Python2.
This is in contrast to if your `kws` dict contains 1, or 3+ keys, which
correctly error loudly and let you know you screwed up, yielding either:
{{{
ValueError: need more than 1 value to unpack
ValueError: too many values to unpack (expected 2)
}}}
Or if you're lucky, and the `get_prep_lookup` can figure out you've been
silly:
{{{
# using kws = {'id': 1, 'is_superuser': False}
ValueError: invalid literal for int() with base 10: 'is_superuser'
}}}
(This last is a relatively recent addition in master, as far as I can
tell, having just pulled up to date from just after 1.8a I think)
The issue itself stems from
[https://github.com/akaariai/django/blob/a7260199ad63cd07e4df85438cb283413978fad2/django/db/models/sql/query.py#L1119
Query.build_filter], and the specific line which causes the problem I
think would survive the changes being made in #24267.
--
Ticket URL: <https://code.djangoproject.com/ticket/24277>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/052.ecc41ae52bdd760a3e2a6a8ee5476951%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.