Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[1.1.X] Fixed #12546. Objects with a __len__ that returns 0 can now b…

…e serialized. Thanks, casobn for the report and Alex Gaynor for the patch and tests. Backport of r12576 from trunk.

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.1.X@12577 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 07f2d19269e1c4ff3b1a2ad04ce5501434488d36 1 parent 692d122
Joseph Kocherhans jkocherhans authored
2  django/db/models/fields/__init__.py
View
@@ -277,7 +277,7 @@ def get_flatchoices(self, include_blank=True, blank_choice=BLANK_CHOICE_DASH):
return first_choice + list(self.flatchoices)
def _get_val_from_obj(self, obj):
- if obj:
+ if obj is not None:
return getattr(obj, self.attname)
else:
return self.get_default()
7 tests/regressiontests/serializers_regress/models.py
View
@@ -252,4 +252,9 @@ class InheritBaseModel(BaseModel):
class ExplicitInheritBaseModel(BaseModel):
parent = models.OneToOneField(BaseModel)
child_data = models.IntegerField()
-
+
+class LengthModel(models.Model):
+ data = models.IntegerField()
+
+ def __len__(self):
+ return self.data
5 tests/regressiontests/serializers_regress/tests.py
View
@@ -8,7 +8,8 @@
"""
-import unittest, datetime
+import datetime
+import unittest
from cStringIO import StringIO
from django.utils.functional import curry
@@ -321,6 +322,8 @@ def inherited_compare(testcase, pk, klass, data):
(inherited_obj, 900, InheritAbstractModel, {'child_data':37,'parent_data':42}),
(inherited_obj, 910, ExplicitInheritBaseModel, {'child_data':37,'parent_data':42}),
(inherited_obj, 920, InheritBaseModel, {'child_data':37,'parent_data':42}),
+ (data_obj, 1004, LengthModel, 0),
+ (data_obj, 1005, LengthModel, 1),
]
# Because Oracle treats the empty string as NULL, Oracle is expected to fail
Please sign in to comment.
Something went wrong with that request. Please try again.