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