-
-
Notifications
You must be signed in to change notification settings - Fork 7k
DateTimeField does not handle empty values correctly (#3726)
#3731
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
Conversation
|
Like #2869 (comment) says maybe we should also check out if |
|
You are correct. I updated the timefield and the test output for timefield also. |
|
FYI - I see that someone added a string conversion in datefield: if (isinstance(value, str)):
value = datetime.datetime.strptime(value, '%Y-%m-%d').date()
I think this is problematic. The datetime, date, and time fields have options to allow the fields to not be required so covering empty options is needed but other than that it should be the correct object i.e. datetime, date or time. Thoughts? If you agree then this pull request should be good to go. |
|
I am not sure what the labels mean? Does the "Needs further review" apply to me or someone else? Are we good on this? |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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'
|
Minor point, but it'd be good if Otherwise I think this is looking good, tho just some clarification on exactly what those tests end up with if we don't include the fix would be helpful. Is it only the blank string case that is currently broken? |
|
Hi Tom - If None is passed in then when it gets to this method (to_representation(self, value)) it (value) is an empty string ''. 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'
|
Issue 3726 DateTimeField not handling empty values
DateTimeField does not handle empty values correctly (#3726)
Closes #3726
Added code to handle None and empty string.
Also added outputs in testing.