[Django] #26219: Cannot filter by DecimalField in RawQuery

47 views
Skip to first unread message

Django

unread,
Feb 14, 2016, 1:09:13 PM2/14/16
to django-...@googlegroups.com
#26219: Cannot filter by DecimalField in RawQuery
----------------------------------------------+----------------------------
Reporter: jirek | Owner: nobody
Type: Bug | Status: new
Component: Database layer (models, ORM) | Version: 1.9
Severity: Normal | Keywords: DecimalField
| RawQuery
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------------------+----------------------------
{{{
#!python

class Product(models.Model):
name = models.CharField(max_length=128)
price = models.DecimalField(max_digits=15, decimal_places=2,
default=0, blank=True)

products = Product.objects.raw(""" SELECT * FROM "products_product" WHERE
price >= %s """, params=[Decimal(200), ])
for product in products:
print product
}}}

this will raise following exception
{{{
#!python

File "site-packages\django\db\models\query.py", line 1219, in __iter__
query = iter(self.query)
File "site-packages\django\db\models\sql\query.py", line 79, in __iter__
self._execute_query()
File "site-packages\django\db\models\sql\query.py", line 106, in
_execute_query
params = tuple(adapter(val) for val in self.params)
File "site-packages\django\db\models\sql\query.py", line 106, in
<genexpr>
params = tuple(adapter(val) for val in self.params)
File "site-packages\django\db\backends\base\operations.py", line 455, in
adapt_unknown_value
return self.adapt_decimalfield_value(value)
TypeError: adapt_decimalfield_value() takes exactly 4 arguments (2 given)
}}}

We are making much more complex query, this is just simple example of bug
we are challenging

--
Ticket URL: <https://code.djangoproject.com/ticket/26219>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Feb 14, 2016, 7:46:32 PM2/14/16
to django-...@googlegroups.com
#26219: Cannot filter by DecimalField in RawQuery
-------------------------------------+-------------------------------------

Reporter: jirek | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.9
(models, ORM) |
Severity: Normal | Resolution:
Keywords: DecimalField | Triage Stage: Accepted
RawQuery |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by jarshwah):

* needs_better_patch: => 0
* needs_docs: => 0
* needs_tests: => 0
* stage: Unreviewed => Accepted


Comment:

Yep, this is indeed a bug. The following patch seems to fix the problem,
but I haven't run the test suite to verify no bad behaviour is introduced.
This is probably a regression introduced in 1.8 (I think). I remember
modifying this code around then.

{{{
diff --git a/django/db/backends/base/operations.py
b/django/db/backends/base/operations.py
index 7957265..10641bf 100644
--- a/django/db/backends/base/operations.py
+++ b/django/db/backends/base/operations.py
@@ -485,7 +485,7 @@ class BaseDatabaseOperations(object):
raise ValueError("Django does not support timezone-aware
times.")
return six.text_type(value)

- def adapt_decimalfield_value(self, value, max_digits,
decimal_places):
+ def adapt_decimalfield_value(self, value, max_digits=None,
decimal_places=None):
"""
Transforms a decimal.Decimal value to an object compatible with
what is
expected by the backend driver for decimal (numeric) columns.

}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/26219#comment:1>

Django

unread,
Feb 14, 2016, 8:10:53 PM2/14/16
to django-...@googlegroups.com
#26219: Cannot filter by DecimalField in RawQuery
-------------------------------------+-------------------------------------

Reporter: jirek | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.9
(models, ORM) |
Severity: Release blocker | Resolution:

Keywords: DecimalField | Triage Stage: Accepted
RawQuery |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by charettes):

* has_patch: 0 => 1
* severity: Normal => Release blocker


Comment:

Ahh we did a bit of concurrent research!

This is regression introduced by d9521f66b1851b0eacd55bc78f801dc64123e333
in 1.9+.

The proposed patch is what I also had in mind.

--
Ticket URL: <https://code.djangoproject.com/ticket/26219#comment:2>

Django

unread,
Feb 15, 2016, 8:50:17 AM2/15/16
to django-...@googlegroups.com
#26219: Cannot filter by DecimalField in RawQuery
-------------------------------------+-------------------------------------

Reporter: jirek | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.9
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: DecimalField | Triage Stage: Accepted
RawQuery |
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* needs_tests: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/26219#comment:3>

Django

unread,
Feb 15, 2016, 1:48:13 PM2/15/16
to django-...@googlegroups.com
#26219: Cannot filter by DecimalField in RawQuery
-------------------------------------+-------------------------------------

Reporter: jirek | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.9
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: DecimalField | Triage Stage: Accepted
RawQuery |
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by charettes):

* easy: 0 => 1


Comment:

Marking as ''easy picking'' since a test can easily be written from the
report.

--
Ticket URL: <https://code.djangoproject.com/ticket/26219#comment:4>

Django

unread,
Feb 16, 2016, 9:29:11 AM2/16/16
to django-...@googlegroups.com
#26219: Cannot filter by DecimalField in RawQuery
-------------------------------------+-------------------------------------
Reporter: jirek | Owner: akki
Type: Bug | Status: assigned

Component: Database layer | Version: 1.9
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: DecimalField | Triage Stage: Accepted
RawQuery |
Has patch: 0 | Needs documentation: 0

Needs tests: 1 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by akki):

* owner: nobody => akki
* status: new => assigned
* has_patch: 1 => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/26219#comment:5>

Django

unread,
Feb 16, 2016, 5:12:48 PM2/16/16
to django-...@googlegroups.com
#26219: Cannot filter by DecimalField in RawQuery
-------------------------------------+-------------------------------------
Reporter: jirek | Owner: akki
Type: Bug | Status: assigned
Component: Database layer | Version: 1.9
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: DecimalField | Triage Stage: Accepted
RawQuery |
Has patch: 1 | Needs documentation: 0

Needs tests: 1 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by akki):

* has_patch: 0 => 1


Comment:

PR: https://github.com/django/django/pull/6150

--
Ticket URL: <https://code.djangoproject.com/ticket/26219#comment:6>

Django

unread,
Feb 17, 2016, 1:57:19 PM2/17/16
to django-...@googlegroups.com
#26219: Cannot filter by DecimalField in RawQuery
-------------------------------------+-------------------------------------
Reporter: jirek | Owner: akki
Type: Bug | Status: closed

Component: Database layer | Version: 1.9
(models, ORM) |
Severity: Release blocker | Resolution: fixed

Keywords: DecimalField | Triage Stage: Accepted
RawQuery |
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham <timograham@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"fdccc02576ae5a524338f65e629948604d80b4c8" fdccc025]:
{{{
#!CommitTicketReference repository=""
revision="fdccc02576ae5a524338f65e629948604d80b4c8"
Fixed #26219 -- Fixed crash when filtering by Decimal in RawQuery.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/26219#comment:7>

Django

unread,
Feb 17, 2016, 2:00:50 PM2/17/16
to django-...@googlegroups.com
#26219: Cannot filter by DecimalField in RawQuery
-------------------------------------+-------------------------------------
Reporter: jirek | Owner: akki
Type: Bug | Status: closed
Component: Database layer | Version: 1.9
(models, ORM) |
Severity: Release blocker | Resolution: fixed
Keywords: DecimalField | Triage Stage: Accepted
RawQuery |
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Tim Graham <timograham@…>):

In [changeset:"0d2b97ca18b576d1bbff2ccb089527e36d99eaf9" 0d2b97ca]:
{{{
#!CommitTicketReference repository=""
revision="0d2b97ca18b576d1bbff2ccb089527e36d99eaf9"
[1.9.x] Fixed #26219 -- Fixed crash when filtering by Decimal in RawQuery.

Backport of fdccc02576ae5a524338f65e629948604d80b4c8 from master
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/26219#comment:8>

Reply all
Reply to author
Forward
0 new messages