Repository: encode/django-rest-framework

Issue:
2.2.2 Breaks Date Fields
Couldn't this have been moved to 2.3?  Seems like a major enough change to not expect breakage from v2.2.1 to v2.2.2 (thank you continuous integration tests!).

```
    return JSONRenderer().render(self.get_serializer_class()(obj).data)
  vim +389 .env/local/lib/python2.7/site-packages/rest_framework/serializers.py  # data
    self._data = self.to_native(obj)
  vim +279 .env/local/lib/python2.7/site-packages/rest_framework/serializers.py  # to_native
    value = field.field_to_native(obj, field_name)
  vim +151 .env/local/lib/python2.7/site-packages/rest_framework/fields.py  # field_to_native
    return self.to_native(value)
  vim +540 .env/local/lib/python2.7/site-packages/rest_framework/fields.py  # to_native
    return value.isoformat()
AttributeError: 'NoneType' object has no attribute 'isoformat'
```



Discussion:
I'm still looking to identify the exact cause of this error and create a reproducible test case, but wanted to bring attention since 2.2.2 just hit PyPI and broke things for me.

It seems to be that my model has an optional DateField(null=True, blank=True) and the field serializer wasn't expecting that option.

In 'to_native(value)' we should start with:

```
if value is None:
    return None
```

On 6 Mar 2013, at 21:43, Kevin Stone notifications@github.com wrote:

> I'm still looking to identify the exact cause of this error and create a reproducible test case, but wanted to bring attention since 2.2.2 just hit PyPI and broke things for me.
> 
> —
> Reply to this email directly or view it on GitHub.


Resolving patch:
diff --git a/rest_framework/fields.py b/rest_framework/fields.py
--- a/rest_framework/fields.py
+++ b/rest_framework/fields.py
@@ -534,6 +534,8 @@ def from_native(self, value):
         raise ValidationError(msg)
 
     def to_native(self, value):
+        if value is None:
+            return None
         if isinstance(value, datetime.datetime):
             value = value.date()
         if self.format.lower() == ISO_8601:
@@ -599,6 +601,8 @@ def from_native(self, value):
         raise ValidationError(msg)
 
     def to_native(self, value):
+        if value is None:
+            return None
         if self.format.lower() == ISO_8601:
             return value.isoformat()
         return value.strftime(self.format)


Test patch:
diff --git a/rest_framework/tests/fields.py b/rest_framework/tests/fields.py
--- a/rest_framework/tests/fields.py
+++ b/rest_framework/tests/fields.py
@@ -171,6 +171,13 @@ def test_to_native_custom_format(self):
 
         self.assertEqual('1984 - 07.31', result_1)
 
+    def test_to_native_none(self):
+        """
+        Make sure from_native() returns None on None param.
+        """
+        f = serializers.DateField(required=False)
+        self.assertEqual(None, f.to_native(None))
+
 
 class DateTimeFieldTest(TestCase):
     """
@@ -303,6 +310,13 @@ def test_to_native_custom_format(self):
         self.assertEqual('1984 - 04:31', result_3)
         self.assertEqual('1984 - 04:31', result_4)
 
+    def test_to_native_none(self):
+        """
+        Make sure from_native() returns None on None param.
+        """
+        f = serializers.DateTimeField(required=False)
+        self.assertEqual(None, f.to_native(None))
+
 
 class TimeFieldTest(TestCase):
     """