One - One Code All

Blog Content

django中json/dict类型数据更新字段

Python   2018-06-03 19:55:27

django中json/dict类型数据更新字段


方法一:


data = {'username':'nick','is_active':'0'}

User.objects.filter(id=1).update(**data)


这种方法不能自动更新具有auto_now属性字段的值

通常我们再变量前加一个星号(*)表示这个变量是元组/列表,加两个星号表示这个参数是字典


方法二:


data = {'username':'nick','is_active':'0'}

_t = User.objects.get(id=1)

_t.__dict__.update(**data)

_t.save()


方法二和方法一同样无法自动更新auto_now字段的值

注意这里使用到了一个dict方法


方法三:


_t = User.objects.get(id=1)

_t.role=Role.objects.get(id=3)

_t.save()



上一篇:django 定义路由的几种方式
下一篇:ValueError: 时间数据与格式 '%Y/%m/%d %H:%M:%S' 不匹配

The minute you think of giving up, think of the reason why you held on so long.