def query(request): |
# 1 all方法:models.表名.objects.all() |
book_all = models.Book.objects. all () # 结果是querySet集合 |
# print(book_all) |
# 2 filter: models.表名.objects.filter() |
ret = models.Book.objects. filter (title = 'PHP' ) # 结果是querySet集合 |
ret2 = models.Book.objects. filter (nid = 1 ) # 结果是querySet集合 |
ret3 = models.Book.objects. filter (author = 'Alex' , price = 35 ) # 结果是querySet集合,且的关系,两个条件都要满足 |
# print(ret) |
# 3 get:models.表名.objects.get() |
ret4 = models.Book.objects.get(nid = 3 ) # model对象,如果取不到值则会报错 |
# print(ret4,ret4.price) |
# 4 exclude:排除条件,取非 |
ret5 = models.Book.objects.exclude(author = 'oldboy' ) |
# 5 values方法 |
# ret6=models.Book.objects.filter(author='Alex').values('title','price') |
# print('ret6',ret6) |
# #6 values_list方法 |
# ret7 = models.Book.objects.filter(author='Alex').values_list('title', 'price') |
# print('ret7', ret7) |
# ret8 = models.Book.objects.filter(author='Alex').values('author').distinct() |
# print('ret8',ret8) |
# 双线划线 |
ret9 = models.Book.objects. filter (price__gt = 30 ) |
ret10 = models.Book.objects. filter (title__startswith = 'P' ) |
ret11 = models.Book.objects. filter (id_lt = 4 , id_gt = 2 ) # id 大于2小于4 |
print ( 'ret10' , ret10) |
return HttpResponse( "OK" ) |