From 07f2d19269e1c4ff3b1a2ad04ce5501434488d36 Mon Sep 17 00:00:00 2001 From: Joseph Kocherhans Date: Wed, 24 Feb 2010 16:06:10 +0000 Subject: [PATCH] [1.1.X] Fixed #12546. Objects with a __len__ that returns 0 can now be 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 --- django/db/models/fields/__init__.py | 2 +- tests/regressiontests/serializers_regress/models.py | 7 ++++++- tests/regressiontests/serializers_regress/tests.py | 5 ++++- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py index d3eee8aeb6..a091183000 100644 --- a/django/db/models/fields/__init__.py +++ b/django/db/models/fields/__init__.py @@ -277,7 +277,7 @@ class Field(object): 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() diff --git a/tests/regressiontests/serializers_regress/models.py b/tests/regressiontests/serializers_regress/models.py index 95119d4b05..628af0e4c4 100644 --- a/tests/regressiontests/serializers_regress/models.py +++ b/tests/regressiontests/serializers_regress/models.py @@ -252,4 +252,9 @@ class InheritBaseModel(BaseModel): class ExplicitInheritBaseModel(BaseModel): parent = models.OneToOneField(BaseModel) child_data = models.IntegerField() - \ No newline at end of file + +class LengthModel(models.Model): + data = models.IntegerField() + + def __len__(self): + return self.data diff --git a/tests/regressiontests/serializers_regress/tests.py b/tests/regressiontests/serializers_regress/tests.py index de7ddcc9f7..0b319d3331 100644 --- a/tests/regressiontests/serializers_regress/tests.py +++ b/tests/regressiontests/serializers_regress/tests.py @@ -8,7 +8,8 @@ forward, backwards and self references. """ -import unittest, datetime +import datetime +import unittest from cStringIO import StringIO from django.utils.functional import curry @@ -321,6 +322,8 @@ The end."""), (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