标签:orm statement color return nta exist oldboy for ret
Django的orm框架对于业务复杂度不是很高的应用来说还是不错的,写起来很方面,用起来也简单。对于新手来说查询操作中最长用的两个方法get和filter有时候一不注意就会犯下一些小错误。那么今天就来小节下这两个方法使用上的不同。
首先对比下两个函数文档上的解释。
get
Returns the object matching the given lookup parameters, which should be in the format described in Field lookups.
get() raises MultipleObjectsReturned if more than one object was found. The MultipleObjectsReturned exception is an attribute of the model class.
get() raises a DoesNotExist exception if an object wasn’t found for the given parameters. This exception is also an attribute of the model class
filter
Returns a new QuerySet containing objects that do not match the given lookup parameters.
The lookup parameters (**kwargs) should be in the format described in Field lookups below. Multiple parameters are joined via AND in the underlying SQL statement, and the whole thing is enclosed in a NOT().
输入参数
get 的参数只能是model中定义的那些字段,只支持严格匹配
filter 的参数可以是字段,也可以是扩展的where查询关键字,如in,like等
返回值
get 返回值是一个定义的model对象
filter 返回值是一个新的QuerySet对象,然后可以对QuerySet在进行查询返回新的QuerySet对象,支持链式操作
QuerySet一个集合对象,可使用迭代或者遍历,切片等,但是不等于list类型(使用一定要注意)
异常
get 只有一条记录返回的时候才正常,也就说明get的查询字段必须是主键或者唯一约束的字段。当返回多条记录或者是没有找到记录的时候都会抛出异常
filter 有没有匹配的记录都可以
单表操作
表记录的修改
方式一:
b=Book.objects.get(author="oldboy")
b.price=120
b.save()
方式二:
#update是QuerySet
Book.objects.filter(author="yuan").update(price=999)
表记录的查询(重点):
book_list = Book.objects.filter(id=2)
book_list=Book.objects.exclude(author="yuan").values("name","price")
book_list=Book.objects.all()
book_list = Book.objects.all()[::2]
book_list = Book.objects.all()[::-1]
#first,last,get取到的是一个实例对象,并非一个QuerySet的集合对象
book_list = Book.objects.first()
book_list = Book.objects.last()
book_list = Book.objects.get(id=2)#只能取出一条记录时才不报错
ret1=Book.objects.filter(author="oldboy").values("name")
ret2=Book.objects.filter(author="yuan").values_list("name","price")
book_list= Book.objects.all().values("name").distinct()
book_count= Book.objects.all().values("name").distinct().count()
模糊查询 双下划线__
book_list=Book.objects.filter(name__icontains="P").values_list("name","price")
book_list=Book.objects.filter(id__gt=5).values_list("name","price")
标签:orm statement color return nta exist oldboy for ret
原文地址:https://www.cnblogs.com/ago-0912/p/10266144.html