Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DateTimeField does not handle empty values correctly (#3726) #3731

Merged
merged 6 commits into from
Dec 18, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions rest_framework/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -1183,6 +1186,9 @@ def to_internal_value(self, value):
self.fail('invalid', format=humanized_format)

def to_representation(self, value):
if not value:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You shouldn't need to handle the None case explicitly, as I believe that's handled as a catch all.

What's the behavior if you don't include this?

Is this behavior in order to catch empty strings and return None for them? (In which case I suspect this may be valid)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Tom - If None is passed in then when it gets to this method it 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'

return None

output_format = getattr(self, 'format', api_settings.TIME_FORMAT)

if output_format is None:
Expand Down
8 changes: 6 additions & 2 deletions tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down Expand Up @@ -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()

Expand Down