Python程序教程

您现在的位置是:首页 >  Django

当前栏目

Django的ORM操作-更新数据

Django,ORM,操作,更新,数据
2025-03-13 21:27:21 时间

更新单个数据


  • 修改单个实体的某些字段值的步骤
  1. 查询:通过get()得到要修改的实体对象
  2. 修改:通过对象的属性方法修改数据
  3. 保存: 通过save()进行保存

进入Django Shell进行操作

# 修改system字段为Ubuntu18.04
from monitor.models import Asset
select = Asset.objects.get(id=1)
select.system="Ubuntu18.04"
select.save()  # 一定要保存,如果不保存不会commit到数据库中

批量更新数据

  • xxxxxxxxxx def del_user_views(request):    if request.method == ‘GET’:        return render(request,‘user/del.html’)        if request.method == ‘POST’:        try:            username = request.POST[‘username’]            console.log(“当前提交删除用户%s”%(username))            del_username = User.objects.filter(username=username)            print(del_username)            del_username.update(is_active=False)        except Exception as e:            return HttpResponse(“当前查询用户%s不存在”%(username))    return HttpResponse(“删除成功”)python
# 更新所有systsm为Windows10的主机系统为Centos7.6
from monitor.models import Asset
select = Asset.objects.filter(system__gte='Windows10')
select.update(system="Centos7.6")