Django“预期的字符串或缓冲区”是什么意思?错误提示?

发布于 2024-10-05 04:11:53 字数 89 浏览 4 评论 0原文

我已经被这个错误困扰了一段时间了,我只是不明白它意味着什么。当我尝试将对象保存到 mysql 数据库时会发生这种情况。有什么想法吗?

感谢您的帮助!

I've been stuck with this error for quite a while now and I just can't figure out what it means. It occurs when I try to save an object to my mysql database. Any ideas?

Thanks for the help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

甜妞爱困 2024-10-12 04:11:53

刚刚遇到了同样的问题并解决了。我实例化了一个这样的表单:

data = {'date' : datetime.now} #this is the problem
form = MyForm(data)

该表单稍后保存,django 尝试在模型中设置“日期”。但显然,datetime.now 指的是函数而不是日期。我想做的是 datetime.now()

也许这可以帮助任何人将来遇到这个问题。

Just ran into the same problem and resolved it. I instantiated a form like this:

data = {'date' : datetime.now} #this is the problem
form = MyForm(data)

That form was saved later on and django tried to set 'date' in the model. But datetime.now refers to a function rather than a date, obviously. What I wanted to do was datetime.now()

Maybe this helps anybody running into this in future.

在巴黎塔顶看东京樱花 2024-10-12 04:11:53

这可能意味着 Python 正在尝试执行需要某种数据类型(bool、string、int 等)的代码,但提供了其他不正确的数据类型。

This probably means that Python is trying to execute code which is expecting a certain datatype (bool, string, int, etc.), but an other, incorrect, datatype is provided.

眉黛浅 2024-10-12 04:11:53

就我而言,当我使用“时间”库将日期字符串转换为日期时间对象时,就会出现这种情况。我只是使用“datetime.strptime”而不是“time.strptime”,问题就消失了。

In my case it was appears when I use "time" library to convert date string to datetime object. I just use "datetime.strptime" instead of "time.strptime"and problem vanished.

玉环 2024-10-12 04:11:53

django 中的日期时间验证器位于文件:

/path/to/project/venv/lib/python2.7/site-packages/django/utils/dateparse.py

或当前 python 解释器的站点包中。
看看那里的正则表达式。就我而言,解决这个问题的方法是:

ended=datetime.fromtimestamp(time.time())
other=datetime.fromtimestamp(time.time())

# in the model:
ended = models.DateTimeField(blank=True, null=True) # or
other = models.DateTimeField(auto_now_add=False, blank=True)

两者都有效。

The datetime validator in django is in the file:

/path/to/project/venv/lib/python2.7/site-packages/django/utils/dateparse.py

or in the site-packages of your current python interpreter.
Have a look there to see the regular expressions. In my case, the way to solve it was:

ended=datetime.fromtimestamp(time.time())
other=datetime.fromtimestamp(time.time())

# in the model:
ended = models.DateTimeField(blank=True, null=True) # or
other = models.DateTimeField(auto_now_add=False, blank=True)

both working.

稀香 2024-10-12 04:11:53

“expiration”是这个字段

expiration = models.DateTimeField(default=7)

,有错误的代码是这个:

ex = timedelta(minutes=expiration)                      
authobj, created = cls.objects.update_or_create(                         
    operator=operator,                                                   
    defaults={'transport':transport,'token':cls.generate_key(40),'expiration':ex}
)                                                                        

通过这样设置(而不是 timedelta,日期)解决了这个问题

ex = datetime.now() + timedelta(minutes=expiration)                      
authobj, created = cls.objects.update_or_create(                         
    operator=operator,                                                   
    defaults={'transport':transport,'token':cls.generate_key(40),'expiration':ex}
)                                                                        

'expiration' is this field

expiration = models.DateTimeField(default=7)

And code with error was this one:

ex = timedelta(minutes=expiration)                      
authobj, created = cls.objects.update_or_create(                         
    operator=operator,                                                   
    defaults={'transport':transport,'token':cls.generate_key(40),'expiration':ex}
)                                                                        

And it was solved by setting it like this (instead of timedelta, a date)

ex = datetime.now() + timedelta(minutes=expiration)                      
authobj, created = cls.objects.update_or_create(                         
    operator=operator,                                                   
    defaults={'transport':transport,'token':cls.generate_key(40),'expiration':ex}
)                                                                        
萌辣 2024-10-12 04:11:53
str(yourvar)

您可以转换您的变量。

str(yourvar)

Ou can convert sting your variables.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文