Opened 11 years ago

Closed 11 years ago

Last modified 7 years ago

#8023 closed (fixed)

Changeset 8131 breaks filtering from the URL in the admin

Reported by: jdetaeye Owned by: Malcolm Tredinnick
Component: Database layer (models, ORM) Version: master
Severity: Keywords:
Cc:… Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


The admin allows filtering objects by edting the url in the browser window.
Eg: http://youraddress/admin/yourapp/yourmodel/?yourdecimalfield__lt=20

When filtering on a decimal field, changeset #7560 breaks this with the following stacktrace:

File "C:\packages\Python-2.5\lib\site-packages\django\core\handlers\" in get_response
  87.                 response = callback(request, *callback_args, **callback_kwargs)
File "C:\packages\Python-2.5\lib\site-packages\django\contrib\admin\views\" in _checklogin
  61.             return view_func(request, *args, **kwargs)
File "c:\develop\frepple\contrib\django\freppledb\utils\" in view_report
  302.              counter = counter.filter( **{smart_str(key): value} )
File "C:\packages\Python-2.5\lib\site-packages\django\db\models\" in filter
  479.         return self._filter_or_exclude(False, *args, **kwargs)
File "C:\packages\Python-2.5\lib\site-packages\django\db\models\" in _filter_or_exclude
  497.             clone.query.add_q(Q(*args, **kwargs))
File "C:\packages\Python-2.5\lib\site-packages\django\db\models\sql\" in add_q
  1176.                         can_reuse=used_aliases)
File "C:\packages\Python-2.5\lib\site-packages\django\db\models\sql\" in add_filter
  1123.         self.where.add((alias, col, field, lookup_type, value), connector)
File "C:\packages\Python-2.5\lib\site-packages\django\db\models\sql\" in add
  48.                 params = field.get_db_prep_lookup(lookup_type, value)
File "C:\packages\Python-2.5\lib\site-packages\django\db\models\fields\" in get_db_prep_lookup
  242.             return [self.get_db_prep_value(value)]
File "C:\packages\Python-2.5\lib\site-packages\django\db\models\fields\" in get_db_prep_value
  736.                                                   self.decimal_places)
File "C:\packages\Python-2.5\lib\site-packages\django\db\backends\" in value_to_db_decimal
  302.         return util.format_number(value, max_digits, decimal_places)
File "C:\packages\Python-2.5\lib\site-packages\django\db\backends\" in format_number
  126.     return u"%.*f" % (decimal_places, value)

Exception Type: TypeError at /admin/input/buffer/
Exception Value: a float is required

The value argument is a Unicode string while the format string only accepts a float.

You can also reproduce the same error from the command line:

>>> works_as_expected = yourapp.yourmodel.objects.all().filter(yourdecimalfield__gt=0)
>>> doesnt_work_any_more = yourapp.yourmodel.objects.all().filter(yourdecimalfield__gt=u'0')

Changing the format string from %f to %s fixes the problem in both backends I tested (SQLite + PostgreSQL):

return u"%.*f" % (decimal_places, value) # OLD
return u"%.*s" % (decimal_places, value) # NEW

Change History (3)

comment:1 Changed 11 years ago by Malcolm Tredinnick

Owner: changed from nobody to Malcolm Tredinnick

comment:2 Changed 11 years ago by Malcolm Tredinnick

Resolution: fixed
Status: newclosed

(In [8143]) Fixed #8023 -- Allow filtering of DecimalFields (in models) using strings.

At the same time, checked all other cases of db_prep_value() to ensure they
aren't making the same mistake.

comment:3 Changed 7 years ago by Jacob

milestone: 1.0 beta

Milestone 1.0 beta deleted

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