python,django,向mysql更新数据时save()报错不能用

水炮 2013-08-08 12:30:54
求大神帮忙,models.py中有个Student类,当我用s = Student(...)创建对象,然后s.save()可以往mysql中插入记录,但是当我用 s.name = 'aaa',想更新的时候,调用s.save()就报错了.错误代码如下,望大家解答一下.

>>> s.save()
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "D:\Program Files (x86)\Python27\lib\site-packages\django\db\models\base.
py", line 431, in save
self.save_base(force_insert=force_insert, force_update=force_update)
File "D:\Program Files (x86)\Python27\lib\site-packages\django\db\models\base.
py", line 495, in save_base
rows = manager.filter(pk=pk_val)._update(values)
File "D:\Program Files (x86)\Python27\lib\site-packages\django\db\models\query
.py", line 446, in _update
query.add_update_fields(values)
File "D:\Program Files (x86)\Python27\lib\site-packages\django\db\models\sql\s
ubqueries.py", line 249, in add_update_fields
val = field.get_db_prep_save(val)
File "D:\Program Files (x86)\Python27\lib\site-packages\django\db\models\field
s\__init__.py", line 202, in get_db_prep_save
return self.get_db_prep_value(value)
File "D:\Program Files (x86)\Python27\lib\site-packages\django\db\models\field
s\__init__.py", line 446, in get_db_prep_value
return self.to_python(value)
File "D:\Program Files (x86)\Python27\lib\site-packages\django\db\models\field
s\__init__.py", line 442, in to_python
ugettext_lazy("This field cannot be null."))
ValidationError: <django.utils.functional.__proxy__ object at 0x0000000002F26320
>
...全文
348 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
CrySleeper 2013-08-08
  • 打赏
  • 举报
回复
贴一些代码吧,感觉好像是你的model object已经离开了执行的上下文
cimi5210 2013-08-08
  • 打赏
  • 举报
回复
你调用save的时候,有的字段值为空了,建议用下面的update方法 ModelName.objects.filter(id=1).update(name='aaa')

37,720

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧