Tac*_*Tac 4 python django django-rest-framework
我正在更新 django 中的数据,但是字符串数据在保存在数据库中时变成了元组字符串。
@api_view(["POST"])
def cate_edit(req):
if not req.user.is_staff:
return HttpResponseNotFound()
data=jsonload(req.body)
if not has(data,["id","title","other_title","introduction"]):
return HttpResponseForbidden()
id=toNumber(data["id"])
if id==None:
return HttpResponseForbidden()
if id==0:
c=Category(
title=data["title"],
other_title=data["other_title"],
introduction=data["introduction"]
)
c.save()
return HttpResponse(c.id)
else:
c=get_object_or_404(Category,id=id)
c.title = data["title"],
c.other_title = data["other_title"],
c.introduction = data["introduction"]
c.save()
return HttpResponse(c.id)
Run Code Online (Sandbox Code Playgroud)
问题发生在最后else,我可以确保数据是有效且正常的字典,例如
{'id': 1, 'title': '1', 'other_title': '2', 'introduction': '3'}
但是在此保存过程之后,数据库中的数据是
title: "('1',)"
other_title:"('2',)"
introduction: '3'
Run Code Online (Sandbox Code Playgroud)
介绍其实是对的。
此外,这是类别的模型
title: "('1',)"
other_title:"('2',)"
introduction: '3'
Run Code Online (Sandbox Code Playgroud)
谢谢
更新:使用 query and 很酷update,但为什么会发生上述情况?我曾经这样做过,但效果很好。
小智 16
作业末尾有逗号。
c.title = data[“title”],
Run Code Online (Sandbox Code Playgroud)
应该:
c.title = data[“title”]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
574 次 |
| 最近记录: |