Opened 7 years ago

Closed 7 years ago

Last modified 3 years ago

#8023 closed (fixed)

Changeset 8131 breaks filtering from the URL in the admin

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

Description

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\base.py" in get_response
  87.                 response = callback(request, *callback_args, **callback_kwargs)
File "C:\packages\Python-2.5\lib\site-packages\django\contrib\admin\views\decorators.py" in _checklogin
  61.             return view_func(request, *args, **kwargs)
File "c:\develop\frepple\contrib\django\freppledb\utils\report.py" in view_report
  302.              counter = counter.filter( **{smart_str(key): value} )
File "C:\packages\Python-2.5\lib\site-packages\django\db\models\query.py" in filter
  479.         return self._filter_or_exclude(False, *args, **kwargs)
File "C:\packages\Python-2.5\lib\site-packages\django\db\models\query.py" 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\query.py" in add_q
  1176.                         can_reuse=used_aliases)
File "C:\packages\Python-2.5\lib\site-packages\django\db\models\sql\query.py" 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\where.py" 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\__init__.py" 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\__init__.py" in get_db_prep_value
  736.                                                   self.decimal_places)
File "C:\packages\Python-2.5\lib\site-packages\django\db\backends\__init__.py" 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\util.py" 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 7 years ago by mtredinnick

  • Needs documentation unset
  • Needs tests unset
  • Owner changed from nobody to mtredinnick
  • Patch needs improvement unset

comment:2 Changed 7 years ago by mtredinnick

  • Resolution set to fixed
  • Status changed from new to closed

(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 3 years ago by jacob

  • milestone 1.0 beta deleted

Milestone 1.0 beta deleted

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