Fixed #5903 -- DecimalField.get_default() now correctly returns a Decimal 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
This commit is contained in:
Brian Rosner 2009-02-11 20:13:17 +00:00
parent e9af96de1e
commit e5cd43e588
3 changed files with 20 additions and 3 deletions

View File

@ -1,8 +1,15 @@
import types import types
import urllib import urllib
import datetime import datetime
from django.utils.functional import Promise 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): class DjangoUnicodeDecodeError(UnicodeDecodeError):
def __init__(self, obj, *args): def __init__(self, obj, *args):
self.obj = obj 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 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 return s
try: try:
if not isinstance(s, basestring,): if not isinstance(s, basestring,):

View File

@ -6,6 +6,11 @@
``QuerySet`` objects to and from "flat" data (i.e. strings). ``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 from django.db import models
class Category(models.Model): class Category(models.Model):
@ -57,6 +62,7 @@ class Actor(models.Model):
class Movie(models.Model): class Movie(models.Model):
actor = models.ForeignKey(Actor) actor = models.ForeignKey(Actor)
title = models.CharField(max_length=50) title = models.CharField(max_length=50)
price = models.DecimalField(max_digits=6, decimal_places=2, default=Decimal('0.00'))
class Meta: class Meta:
ordering = ('title',) ordering = ('title',)
@ -194,7 +200,7 @@ __test__ = {'API_TESTS':"""
# Let's serialize our movie # Let's serialize our movie
>>> print serializers.serialize("json", [mv]) >>> 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 # Deserialization of movie
>>> list(serializers.deserialize('json', serializers.serialize('json', [mv])))[0].object.title >>> 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 # Primary key is None in case of not saved model
>>> mv2 = Movie(title="Movie 2", actor=ac) >>> mv2 = Movie(title="Movie 2", actor=ac)
>>> print serializers.serialize("json", [mv2]) >>> 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 # Deserialization of null returns None for pk
>>> print list(serializers.deserialize('json', serializers.serialize('json', [mv2])))[0].object.id >>> print list(serializers.deserialize('json', serializers.serialize('json', [mv2])))[0].object.id

View File

@ -20,6 +20,10 @@ Traceback (most recent call last):
... ...
ValidationError: This value must be a decimal number. 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) >>> f = DecimalField(max_digits=5, decimal_places=1)
>>> x = f.to_python(2) >>> x = f.to_python(2)
>>> y = f.to_python('2.6') >>> y = f.to_python('2.6')