一、简介
二、API方法
1.filter(**kwargs)
- 返回一个新的QuerySet,它包含了与所给的筛选条件相匹配的对象。
ret = Book.objects.filter(publish="苹果出版社")
ret
<QuerySet [<Book: Book object (9)>, <Book: Book object (10)>, <Book: Book object (11)>]>
ret[0].title
'NodeJS'
2.get(**kwargs)
3.exclude(**kwargs)
- 返回一个QuerySet对象,指不包含匹配对象的所有值,filter的反义词。
ret = Book.objects.exclude(publish="苹果出版社")
ret
<QuerySet [<Book: Book object (5)>, <Book: Book object (7)>, <Book: Book object (8)>, <Book: Book object (12)>, <Book: Book object (13)>, <Book: Book object (14)>]>
- 其返回的也是一个queryset对象,先通过切片后取值
ret[0].title
'go'
4.order_by(**kwargs)
- 返回一个queryset对象,通过某个字段进行排序
ret = Book.objects.order_by("price")
ret
<QuerySet [<Book: Book object (9)>, <Book: Book object (10)>, <Book: Book object (11)>, <Book: Book object (14)>, <Book: Book object (5)>, <Book: Book object (7)>, <Book: Book object (8)>, <Book: Book object (13)>, <Book: Book object (12)>]>
- 切片后调取值
ret[0].title
'NodeJS'
5.reverse(**kwargs)
- 返回一个queryset对象,对查询结果进行反序。首先它需要有一个ordey_by排序
ret = Book.objects.order_by("price").reverse()
ret
<QuerySet [<Book: Book object (12)>, <Book: Book object (5)>, <Book: Book object (7)>, <Book: Book object (8)>, <Book: Book object (13)>, <Book: Book object (14)>, <Book: Book object (11)>, <Book: Book object (10)>, <Book: Book object (9)>]>
- 切片后调取值
ret[0].title
'红楼梦'
6.values(**kwargs)
- 返回一个queryset对象,打印其所有值。
ret = Book.objects.filter(id=5).values()
ret
<QuerySet [{'id': 5, 'title': 'go', 'price': Decimal('123.00'), 'publish': '华夏出版社', 'pub_date': datetime.date(2019, 11, 5)}]>
- 切片后获取其值
ret[0]
{'id': 5, 'title': 'go', 'price': Decimal('123.00'), 'publish': '华夏出版社', 'pub_date': datetime.date(2019, 11, 5)}
ret[0]["title"]
留言