Skip to content

Commit

Permalink
Fixed #12546. Objects with a __len__ that returns 0 can now be serial…
Browse files Browse the repository at this point in the history
…ized. Thanks, casobn for the report and Alex Gaynor for the patch and tests.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@12576 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
jkocherhans committed Feb 24, 2010
1 parent ae43f65 commit 9f4bf52
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
2 changes: 1 addition & 1 deletion django/db/models/fields/__init__.py
Expand Up @@ -404,7 +404,7 @@ def get_flatchoices(self, include_blank=True, blank_choice=BLANK_CHOICE_DASH):
return first_choice + list(self.flatchoices) return first_choice + list(self.flatchoices)


def _get_val_from_obj(self, obj): def _get_val_from_obj(self, obj):
if obj: if obj is not None:
return getattr(obj, self.attname) return getattr(obj, self.attname)
else: else:
return self.get_default() return self.get_default()
Expand Down
6 changes: 6 additions & 0 deletions tests/regressiontests/serializers_regress/models.py
Expand Up @@ -258,3 +258,9 @@ class InheritBaseModel(BaseModel):
class ExplicitInheritBaseModel(BaseModel): class ExplicitInheritBaseModel(BaseModel):
parent = models.OneToOneField(BaseModel) parent = models.OneToOneField(BaseModel)
child_data = models.IntegerField() child_data = models.IntegerField()

class LengthModel(models.Model):
data = models.IntegerField()

def __len__(self):
return self.data
10 changes: 5 additions & 5 deletions tests/regressiontests/serializers_regress/tests.py
Expand Up @@ -8,7 +8,9 @@
""" """




import unittest, datetime import datetime
import decimal
import unittest
from cStringIO import StringIO from cStringIO import StringIO


from django.utils.functional import curry from django.utils.functional import curry
Expand All @@ -18,10 +20,6 @@
from django.conf import settings from django.conf import settings


from models import * from models import *
try:
import decimal
except ImportError:
from django.utils import _decimal as decimal


# A set of functions that can be used to recreate # A set of functions that can be used to recreate
# test data objects of various kinds. # test data objects of various kinds.
Expand Down Expand Up @@ -326,6 +324,8 @@ def inherited_compare(testcase, pk, klass, data):
(data_obj, 1001, BigIntegerData, -9223372036854775808), (data_obj, 1001, BigIntegerData, -9223372036854775808),
(data_obj, 1002, BigIntegerData, 0), (data_obj, 1002, BigIntegerData, 0),
(data_obj, 1003, BigIntegerData, None), (data_obj, 1003, BigIntegerData, None),
(data_obj, 1004, LengthModel, 0),
(data_obj, 1005, LengthModel, 1),
] ]


# Because Oracle treats the empty string as NULL, Oracle is expected to fail # Because Oracle treats the empty string as NULL, Oracle is expected to fail
Expand Down

0 comments on commit 9f4bf52

Please sign in to comment.