我有这个型号:
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框架):
@detail_route(methods=['POST'], permission_classes= [IsOwnerOrReadOnly])
def set_confidential(self, request, pk=None):
comment = self.get_object()
if(request.data.get("booleanField", None) != None):
comment.confidential = request.data["booleanField"];
comment.save()
return Response({'from':'set_confidential','status':_("Confidential status changed")})
else:
return Response({"status":status.HTTP_400_BAD_REQUEST})
问题在于,当我通过" False"该视图的值是我的对象更新但字段"机密"仍然是真的(假设之前就是这样)。为什么会这样?
答案 0 :(得分:3)
您应该保存comment
对象。请注意,在布尔上下文中,任何非空字符串都被视为True
:
comment.confidential = (request.data["booleanField"].lower() == 'true')
comment.save()