Я обновляю данные в 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)
Проблема возникла в финале else
, я могу убедиться, что данныедействительный и нормальный dict, такой как {'id': 1, 'title': '1', 'other_title': '2', 'introduction': '3'}
, но после этого процесса сохранения данные в базе данных будут
title: "('1',)"
other_title:"('2',)"
introduction: '3'
. Введение действительно корректно.
Кроме того, вот модель категории
class Category(models.Model):
title = models.CharField(max_length=50)
other_title = models.CharField(max_length=50,blank=True)
image = models.ImageField(blank=True,null=True,upload_to=file_path)
introduction = models.TextField(default="",blank=True)
last_modified = models.DateTimeField(auto_now=True)
def __str__(self):
return self.title
Спасибо
Обновление: классно использовать запрос и update
, но почему вышеописанная ситуация происходит?Раньше так делал, но отлично работает.