Django

Code

Ticket #8023 (closed: fixed)

Opened 1 month ago

Last modified 1 month ago

Changeset 8131 breaks filtering from the URL in the admin

Reported by: jdetaeye Assigned to: mtredinnick
Milestone: 1.0 beta Component: Database wrapper
Version: SVN Keywords:
Cc: johan.de.taeye@gmail.com Triage Stage: Unreviewed
Has patch: 0 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

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

Attachments

Change History

07/29/08 14:07:50 changed by mtredinnick

  • owner changed from nobody to mtredinnick.
  • needs_better_patch changed.
  • needs_tests changed.
  • needs_docs changed.

07/29/08 19:18:50 changed by mtredinnick

  • status changed from new to closed.
  • resolution set to fixed.

(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.


Add/Change #8023 (Changeset 8131 breaks filtering from the URL in the admin)




Change Properties
Action