Python django对象中的save方法未保存布尔模型字段,如果传递为false

Python django对象中的save方法未保存布尔模型字段,如果传递为false,python,django,request,django-rest-framework,Python,Django,Request,Django Rest Framework,我有这个模型: class Comment(models.Model): comment_text = models.TextField("Comentário") user = models.ForeignKey(Profile) created = models.DateTimeField(auto_now_add=True, verbose_name="Criação") updated = models.DateTimeField(auto_now=Tru

我有这个模型:

class Comment(models.Model):
    comment_text = models.TextField("Comentário")
    user = models.ForeignKey(Profile)
    created = models.DateTimeField(auto_now_add=True, verbose_name="Criação")
    updated = models.DateTimeField(auto_now=True, verbose_name="Última modificação")
    confidential = models.BooleanField("Confidencial", default=False)
我有一个视图集(使用rest框架):


问题是,当我将“False”值传递给该视图时,我的对象将被更新,但“机密”字段仍为True(假设它与以前一样)。为什么会发生这种情况?

您应该保存
注释
对象。请注意,在布尔上下文中,任何非空字符串都被视为
True

comment.confidential = (request.data["booleanField"].lower() == 'true')
comment.save()

谢谢@catavaran。我以前放过comment.save()。我在这里发帖时不小心擦掉了。如果booleanField中的值为False,则仍然使用comment.save(),comment.confidential不会更改。现在我意识到,当转换为bool时,request.data的值[“booleanField”](“bool(request.data[“booleanField”]”)总是返回True,即使booleanField的值为“False”.python如何将字符串转换为bool?python中内置函数bool的doc是这样说的:当参数x为True时返回True,否则返回False。内置的True和False是bool类的仅有两个实例。bool类是int类的子类,不能被子类化。
comment.confidential = (request.data["booleanField"].lower() == 'true')
comment.save()