查找所有
students = Student.object.all()
查找名字是张三的
name = Student.objects.filter(name='张三')
多个条件查找
books = Book.objects.filter(name='三国演义',desc='test')
单个条件查找
book = Book.objects.get(name='三国演义')
filter和get区别:
filter找不到会返回空列表
get找不到会报错
**排序 order_by 从小到大 **
books = Book.objects.order_by("pub_date")
倒序可以加负号
books = Book.objects.order_by("-pub_date")
修改内容的方法:先查找出来。再更改值,最后保存
book = Book.objects.get(name='三国演义')
book.pub_date = datetime.now()
book.save()
删除:先查找在用delete
book = Book.objects.get(name='三国演义')
book.delete()
聚合函数
avg:求平均值
result = Book.objects.aggregate(Avg('price'))
count :获取指定的对象的个数
result = Book.objects.aggregate(book_num=Count('id'))
Max 和 Min :获取指定对象的最大值和最小值
result = Author.objects.aggregate(Max('age'),Min('age'))