Django DateTimeField TypeError:预期的字符串或类似字节的对象 [英] Django DateTimeField TypeError: expected string or bytes-like object

查看:291
本文介绍了Django DateTimeField TypeError:预期的字符串或类似字节的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道关于此错误的帖子很多。例如: this 这一个

I know there are many posts about this error. Such as: this and this one

但是我已经知道了。这就是为什么我变得疯狂。

But I knew already that. And that's why I'm getting crazy.

当我创建实例并尝试保存它时,我没有遇到问题:

When I create an instance and try to save it, I don't have problems:

per_detail.preview_title = per_detail.details_sample.preview_title
per_detail.icon = per_detail.details_sample.icon
per_detail.content = fill_content(per_detail)
per_detail.save()

简单。

但是当已经实例,然后尝试再次保存(更新)。然后我得到这个错误:

But when there's already the instance, and I try to save it again (updated it). Then I get this error:


match = datetime_re.match(value)

TypeError:预期的字符串或类似字节的对象

TypeError: expected string or bytes-like object

输入以下代码:

personal_detail_sample = kwargs['instance']
        personal_details = PersonalDetail.objects.filter(Q(details_sample=personal_detail_sample))
        for per_detail in personal_details:
            per_detail_updated = fill_updated_content(personal_detail_sample, per_detail)
            per_detail_updated.save()

我得说,在哪里更新该实例都没关系,我总是得到相同的错误。 (所以,这不是因为 kwargs ['instance']

I gotta say, it doesn't matter where I try to update that instance, I get always the same error. (so, it's not because the kwargs['instance'])

这是该字段,麻烦:

sent_date = models.DateTimeField(_('sent_date'),null = True,blank = True)

您可能会注意到,我从未填写过 sent_date字段,但该字段应为null。因此,这应该不是问题。为了安全起见,我还尝试这样做:

As you maybe noticed, I never filled the field "sent_date", but it should be null. So it shouldn't be a problem. And for just being safe, I also tried to do:

per_detail.sent_date = *a date*
per_detail.save()

我遇到了同样的错误。

And I'm getting the same error.

我不知道会是什么。

也许有人可以帮助我。

Maybe someone can help me.

推荐答案

我在日期字段而不是发送日期中出现错误。

I got an error in the field "date" not "sent_date".

非常抱歉。 models.py 中的日期字段为:

I'm pretty sorry. The field of "date" in the models.py was:

date = models.DateTimeField(_('日期),auto_now_add = True),但在数据库中是没有时区的时间而不是没有时区的时间戳。为什么?谁知道...

date = models.DateTimeField(_('date'), auto_now_add=True) but in the DataBase was time without time zone and not timestamp without time zone. Why? Who knows...

我删除了 sent_date 字段,发现日期还有另一个错误,这就是为什么我发现错误。

I deleted the sent_date field, and saw, that there was another error with a date, and that's why I found the error.

非常感谢尝试提供帮助的人!

Many thanks to the people that tried to help!

这篇关于Django DateTimeField TypeError:预期的字符串或类似字节的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆