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 #7139

Closed
py2010 opened this issue Jan 13, 2020 · 0 comments
Closed

DateTimeField does not handle empty values correctly #3726 #7139

py2010 opened this issue Jan 13, 2020 · 0 comments

Comments

@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)

#3726

@py2010 py2010 closed this as completed Jan 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant