diff --git a/django/utils/encoding.py b/django/utils/encoding.py index dbd2c7a211..7e87b05c79 100644 --- a/django/utils/encoding.py +++ b/django/utils/encoding.py @@ -1,8 +1,15 @@ import types import urllib import datetime + from django.utils.functional import Promise +try: + from decimal import Decimal +except ImportError: + from django.utils._decimal import Decimal # Python 2.3 fallback + + class DjangoUnicodeDecodeError(UnicodeDecodeError): def __init__(self, obj, *args): self.obj = obj @@ -41,7 +48,7 @@ def force_unicode(s, encoding='utf-8', strings_only=False, errors='strict'): If strings_only is True, don't convert (some) non-string-like objects. """ - if strings_only and isinstance(s, (types.NoneType, int, long, datetime.datetime, datetime.date, datetime.time, float)): + if strings_only and isinstance(s, (types.NoneType, int, long, datetime.datetime, datetime.date, datetime.time, float, Decimal)): return s try: if not isinstance(s, basestring,): diff --git a/tests/modeltests/serializers/models.py b/tests/modeltests/serializers/models.py index f9130d1dfc..eda19ecc4f 100644 --- a/tests/modeltests/serializers/models.py +++ b/tests/modeltests/serializers/models.py @@ -6,6 +6,11 @@ ``QuerySet`` objects to and from "flat" data (i.e. strings). """ +try: + from decimal import Decimal +except ImportError: + from django.utils._decimal import Decimal # Python 2.3 fallback + from django.db import models class Category(models.Model): @@ -57,6 +62,7 @@ class Actor(models.Model): class Movie(models.Model): actor = models.ForeignKey(Actor) title = models.CharField(max_length=50) + price = models.DecimalField(max_digits=6, decimal_places=2, default=Decimal('0.00')) class Meta: ordering = ('title',) @@ -194,7 +200,7 @@ __test__ = {'API_TESTS':""" # Let's serialize our movie >>> print serializers.serialize("json", [mv]) -[{"pk": 1, "model": "serializers.movie", "fields": {"actor": "Za\u017c\u00f3\u0142\u0107", "title": "G\u0119\u015bl\u0105 ja\u017a\u0144"}}] +[{"pk": 1, "model": "serializers.movie", "fields": {"price": "0.00", "actor": "Za\u017c\u00f3\u0142\u0107", "title": "G\u0119\u015bl\u0105 ja\u017a\u0144"}}] # Deserialization of movie >>> list(serializers.deserialize('json', serializers.serialize('json', [mv])))[0].object.title @@ -204,7 +210,7 @@ u'G\u0119\u015bl\u0105 ja\u017a\u0144' # Primary key is None in case of not saved model >>> mv2 = Movie(title="Movie 2", actor=ac) >>> print serializers.serialize("json", [mv2]) -[{"pk": null, "model": "serializers.movie", "fields": {"actor": "Za\u017c\u00f3\u0142\u0107", "title": "Movie 2"}}] +[{"pk": null, "model": "serializers.movie", "fields": {"price": "0.00", "actor": "Za\u017c\u00f3\u0142\u0107", "title": "Movie 2"}}] # Deserialization of null returns None for pk >>> print list(serializers.deserialize('json', serializers.serialize('json', [mv2])))[0].object.id diff --git a/tests/regressiontests/model_fields/tests.py b/tests/regressiontests/model_fields/tests.py index 80ff4bada6..b1a572844f 100644 --- a/tests/regressiontests/model_fields/tests.py +++ b/tests/regressiontests/model_fields/tests.py @@ -20,6 +20,10 @@ Traceback (most recent call last): ... ValidationError: This value must be a decimal number. +>>> f = DecimalField(default=Decimal("0.00")) +>>> f.get_default() +Decimal("0.00") + >>> f = DecimalField(max_digits=5, decimal_places=1) >>> x = f.to_python(2) >>> y = f.to_python('2.6')