diff options
| -rw-r--r-- | django/db/models/base.py | 4 | ||||
| -rw-r--r-- | django/db/models/fields/__init__.py | 32 | ||||
| -rw-r--r-- | django/template/__init__.py | 5 |
3 files changed, 38 insertions, 3 deletions
diff --git a/django/db/models/base.py b/django/db/models/base.py index a5c99865a6..f77d376c9a 100644 --- a/django/db/models/base.py +++ b/django/db/models/base.py @@ -260,11 +260,11 @@ class Model(object): # is *not* consumed. We rely on this, so don't change the order # without changing the logic. for val, field in izip(args, fields_iter): - setattr(self, field.attname, val) + setattr(self, field.attname, field.localize(val)) else: # Slower, kwargs-ready version. for val, field in izip(args, fields_iter): - setattr(self, field.attname, val) + setattr(self, field.attname, field.localize(val)) kwargs.pop(field.name, None) # Maintain compatibility with existing calls. if isinstance(field.rel, ManyToOneRel): diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py index a3007a7f66..55f5f5f032 100644 --- a/django/db/models/fields/__init__.py +++ b/django/db/models/fields/__init__.py @@ -339,6 +339,34 @@ class Field(object): "Returns the value of this field in the given model instance." return getattr(obj, self.attname) + def localize(self, value): + """ + Returns the value with a localize method that can be used when + representing the object as a string, to get the value formatted + using the current locale + """ + # FIXME: is there a way to to cast an instance to a class that works for + # datetime.datetime objects? + import datetime + localized_class = type('localized_class', (type(value),), dict(localize=None)) + if isinstance(value, datetime.date): + localized_value = localized_class(value.year, value.month, value.day) + elif isinstance(value, datetime.datetime): + localized_value = localized_class(value.year, value.month, value.day, + value.hour, value.minute, value.second) + else: + localized_value = localized_class(value) + + localized_value.localize = lambda : self._localize_funct(value) + return localized_value + + def _localize_funct(self, value): + """ + Localizable field types should overwrite this method to specify + how to localize field representation + """ + return unicode(value) + class AutoField(Field): empty_strings_allowed = False def __init__(self, *args, **kwargs): @@ -636,6 +664,10 @@ class DecimalField(Field): defaults.update(kwargs) return super(DecimalField, self).formfield(**defaults) + def _localize_funct(self, value): + from django.utils.formats import number_format + return number_format(value) + class EmailField(CharField): def __init__(self, *args, **kwargs): kwargs['max_length'] = kwargs.get('max_length', 75) diff --git a/django/template/__init__.py b/django/template/__init__.py index 5493e5bbb7..ec4c6a8d62 100644 --- a/django/template/__init__.py +++ b/django/template/__init__.py @@ -744,7 +744,10 @@ class Variable(object): else: raise - return current + if hasattr(current, 'localize'): + return current.localize() + else: + return current class Node(object): # Set this to True for nodes that must be first in the template (although |
