Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #5903 -- DecimalField.get_default() now correctly returns a Dec…

…imal object when the model instance was not retrieved from the database. Thanks Justin Driscoll and pigletto.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@9823 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit e5cd43e5884ec5860cb7f421b6f84eda93e70b3b 1 parent e9af96d
Brian Rosner brosner authored
9 django/utils/encoding.py
View
@@ -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,):
10 tests/modeltests/serializers/models.py
View
@@ -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 @@ def __unicode__(self):
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 @@ class Score(models.Model):
# 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 @@ class Score(models.Model):
# 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
4 tests/regressiontests/model_fields/tests.py
View
@@ -20,6 +20,10 @@
...
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')
Please sign in to comment.
Something went wrong with that request. Please try again.