DateTimeField
does not handle empty values correctly (#3726) by mjparker777 · Pull Request #3731 · encode/django-rest-framework (original) (raw)
Hi Tom -
Modified datefield to match the other two.
If None is passed in then when it gets to this method (to_representation(self, value)) it (value) is an empty string ''.
With completed = serializers.DateTimeField(required=False) and completed is None you see this output:
2015-12-17 18:58:53,631 ERROR RequestDetailsHandler::get::371 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 1071, in to_representation value = value.isoformat() AttributeError: 'str' object has no attribute 'isoformat'