djangoORM操作

表操作使用django集成的方法,

比如有一张User表:User.objects.all()  返回一个Queryset对象(数据类型list),里面是User表的所有行封装成的User对象。

User.objects是一个QuerySet实例。django查找一个对象显然是先查看是否内存中已经有了此对象,如果没有再去数据库查看。

增删改查的方法:

  增:objects.create()方法,此方法直接修改数据库

  改:updata()方法,updata_or_create()方法,select_for_update()

updata()

"""
更新当前QuerySet中的所有元素,将所有给定字段设置为适当的值
"""

 

select_for_update()

"""
返回一个新的QueReSET实例,该实例将选择更新锁定的对象。
"""

updata_or_create()

"""
Look up an object with the given kwargs, updating one with defaults
if it exists, otherwise create a new one.
Return a tuple (object, created), where created is a boolean
specifying whether an object was created.
"""
以上几个方法都需要save()

删:

猜你喜欢

转载自www.cnblogs.com/yuanji2018/p/10033957.html