Ticket #6783: django-DecimalField-prep_locale.patch

File django-DecimalField-prep_locale.patch, 1.5 KB (added by Dirk Datzert <dummy@…>, 17 years ago)

short work-around for DecimalField format problems in db_prep methods

  • django/db/models/fields/__init__.py

     
    66except ImportError:
    77    from django.utils import _decimal as decimal    # for Python 2.3
    88
     9from locale import getlocale, setlocale, LC_NUMERIC
     10
    911from django.db import get_creation_module
    1012from django.db.models import signals
    1113from django.dispatch import dispatcher
     
    684686        return u"%.*f" % (self.decimal_places, value)
    685687
    686688    def get_db_prep_save(self, value):
     689        self._prep_locale()
    687690        if value is not None:
    688691            value = self._format(value)
     692        self._prep_restorelocale()
    689693        return super(DecimalField, self).get_db_prep_save(value)
    690694
    691695    def get_db_prep_lookup(self, lookup_type, value):
     696        self._prep_locale()
    692697        if lookup_type == 'range':
    693698            value = [self._format(v) for v in value]
    694699        else:
    695700            value = self._format(value)
     701        self._prep_restorelocale()
    696702        return super(DecimalField, self).get_db_prep_lookup(lookup_type, value)
    697703
     704    def _prep_locale():
     705        self._prep_current_locale = getlocale()[0]
     706        setlocale(LC_NUMERIC, 'en_EN')
     707
     708    def _prep_restorelocale():
     709        setlocale(LC_NUMERIC, self._prep_current_locale)
     710
    698711    def get_manipulator_field_objs(self):
    699712        return [curry(oldforms.DecimalField, max_digits=self.max_digits, decimal_places=self.decimal_places)]
    700713
Back to Top