Repository: encode/django-rest-framework

Issue:
`DateTimeField` does not handle empty values correctly
Reference:
https://github.com/tomchristie/django-rest-framework/pull/2869
https://github.com/tomchristie/django-rest-framework/issues/2687

This issue was fixed by 2869 for DateField and need the same fix for DateTimeField.

<pre>
    def to_representation(self, value):
        if not value:
            return None
</pre>

If I have a serializer with
start = serializers.DateTimeField(required=False)
then I should not have to supply a value but if I dont supply a value I get the below error.

<pre>
2015-12-12 00:31:18,347 ERROR RequestDetailsHandler::get::371 <type 'exceptions.AttributeError'>
Traceback (most recent call last):
  File "/x/local/aitv2/automatic_integration_v2/eci/handlers/RequestDetailsHandler.py", line 368, in get
    return response.Response(RequestDetailsResponseSerializer(response_data).data,
  File "/Users/miparker/.virtualenvs/aitv3/lib/python2.7/site-packages/rest_framework/serializers.py", line 503, in data
    ret = super(Serializer, self).data
  File "/Users/miparker/.virtualenvs/aitv3/lib/python2.7/site-packages/rest_framework/serializers.py", line 239, in data
    self._data = self.to_representation(self.instance)
  File "/Users/miparker/.virtualenvs/aitv3/lib/python2.7/site-packages/rest_framework/serializers.py", line 472, in to_representation
    ret[field.field_name] = field.to_representation(attribute)
  File "/Users/miparker/.virtualenvs/aitv3/lib/python2.7/site-packages/rest_framework/serializers.py", line 614, in to_representation
    self.child.to_representation(item) for item in iterable
  File "/Users/miparker/.virtualenvs/aitv3/lib/python2.7/site-packages/rest_framework/serializers.py", line 472, in to_representation
    ret[field.field_name] = field.to_representation(attribute)
  File "/Users/miparker/.virtualenvs/aitv3/lib/python2.7/site-packages/rest_framework/serializers.py", line 472, in to_representation
    ret[field.field_name] = field.to_representation(attribute)
  File "/Users/miparker/.virtualenvs/aitv3/lib/python2.7/site-packages/rest_framework/fields.py", line 1068, in to_representation
    value = value.isoformat()
AttributeError: 'str' object has no attribute 'isoformat'
</pre>



Discussion:

Resolving patch:
diff --git a/rest_framework/fields.py b/rest_framework/fields.py
--- a/rest_framework/fields.py
+++ b/rest_framework/fields.py
@@ -1061,6 +1061,9 @@ def to_internal_value(self, value):
         self.fail('invalid', format=humanized_format)
 
     def to_representation(self, value):
+        if not value:
+            return None
+
         output_format = getattr(self, 'format', api_settings.DATETIME_FORMAT)
 
         if output_format is None:
@@ -1118,11 +1121,11 @@ def to_internal_value(self, value):
         self.fail('invalid', format=humanized_format)
 
     def to_representation(self, value):
-        output_format = getattr(self, 'format', api_settings.DATE_FORMAT)
-
         if not value:
             return None
 
+        output_format = getattr(self, 'format', api_settings.DATE_FORMAT)
+
         if output_format is None:
             return value
 
@@ -1183,6 +1186,9 @@ def to_internal_value(self, value):
         self.fail('invalid', format=humanized_format)
 
     def to_representation(self, value):
+        if not value:
+            return None
+
         output_format = getattr(self, 'format', api_settings.TIME_FORMAT)
 
         if output_format is None:


Test patch:
diff --git a/tests/test_fields.py b/tests/test_fields.py
--- a/tests/test_fields.py
+++ b/tests/test_fields.py
@@ -959,7 +959,9 @@ class TestDateTimeField(FieldValues):
     }
     outputs = {
         datetime.datetime(2001, 1, 1, 13, 00): '2001-01-01T13:00:00',
-        datetime.datetime(2001, 1, 1, 13, 00, tzinfo=timezone.UTC()): '2001-01-01T13:00:00Z'
+        datetime.datetime(2001, 1, 1, 13, 00, tzinfo=timezone.UTC()): '2001-01-01T13:00:00Z',
+        None: None,
+        '': None,
     }
     field = serializers.DateTimeField(default_timezone=timezone.UTC())
 
@@ -1028,7 +1030,9 @@ class TestTimeField(FieldValues):
         '99:99': ['Time has wrong format. Use one of these formats instead: hh:mm[:ss[.uuuuuu]].'],
     }
     outputs = {
-        datetime.time(13, 00): '13:00:00'
+        datetime.time(13, 00): '13:00:00',
+        None: None,
+        '': None,
     }
     field = serializers.TimeField()