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

Closed
mjparker777 opened this issue Dec 12, 2015 · 3 comments
Closed

DateTimeField does not handle empty values correctly #3726

mjparker777 opened this issue Dec 12, 2015 · 3 comments
Labels
Milestone

Comments

@mjparker777
Copy link
Contributor

Reference:
#2869
#2687

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

    def to_representation(self, value):
        if not value:
            return None

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.

2015-12-12 00:31:18,347 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 1068, in to_representation
    value = value.isoformat()
AttributeError: 'str' object has no attribute 'isoformat'
@mjparker777
Copy link
Contributor Author

I made the needed changes and created a pull request. Includes outputs for tests.
#3731

@mjparker777
Copy link
Contributor Author

All checks have passed, ready for review and merge.

@py2010
Copy link

py2010 commented Jan 13, 2020

django 2.2.*
rest_framework 3.9.1
multipart/form-data POST (have files upload)
DateTimeField "required": false,

from rest_framework import fields

def rewrite(cls, func='to_internal_value'):
    # 重写函数,使提交保存的日期时间字段为""时,不报格式错误
    old_func = getattr(cls, func)

    def new_func(self, value):
        # import ipdb; ipdb.set_trace()
        if value == '':
            # return self.get_default()  # 旧数据原值不变或新数据取默认值
            return None  # 表字段数据设置为null
        return old_func(self, value)
    setattr(cls, func, new_func)
    # print(cls, '已重写函数', func)

rewrite(fields.DateTimeField)
rewrite(fields.DateField)
rewrite(fields.TimeField)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants