- distinct()
distinct()
方法用于对数据的去重,返回QuerySet
数据类型- 注意:
- 对模型类的对象去重没有意义,因为每个对象都是一个不一样的存在
distinct()
一般都是联合values
或者values_list
使用
- 实例:
# -*- coding: utf-8 -*- from statistics import mode from django.shortcuts import render,HttpResponse from app1_model import models def add_book(request): #books = models.Book.objects.create(title="九阴神功",price=200,publish="功夫出版社",pub_date="1777-10-10") books = models.Book.objects.values_list("publish").distinct() #books = models.Book.objects.distinct() print(books) return HttpResponse('<p>查找成功!</p>')
- 访问测试
# -*- coding: utf-8 -*- from statistics import mode from django.shortcuts import render,HttpResponse from app1_model import models def add_book(request): #books = models.Book.objects.create(title="九阴神功",price=200,publish="功夫出版社",pub_date="1777-10-10") #books = models.Book.objects.values_list("publish").distinct() books = models.Book.objects.distinct() print(books) return HttpResponse('<p>查找成功!</p>')
访问测试
(3)exclude()和filter()的条件查询
filter()
方法基于双下划线的模糊查询,exclude也是同理- 注意:
filter中运算符只能使用等于号:
=
,不能使用大于、小于号以及其他符号
__in
__in
用于读取区间,而=
后面为列表
# -*- coding: utf-8 -*- from statistics import mode from django.shortcuts import render,HttpResponse from app1_model import models def add_book(request): #books = models.Book.objects.create(title="九阴神功",price=200,publish="功夫出版社",pub_date="1777-10-10") books = models.Book.objects.filter(price__in=[200,300]) print(books) return HttpResponse('<p>查找成功!</p>')
访问测试
__gt
__gt
大于,=
号后面为数字
# -*- coding: utf-8 -*- from statistics import mode from django.shortcuts import render,HttpResponse from app1_model import models def add_book(request): #books = models.Book.objects.create(title="九阴神功",price=200,publish="功夫出版社",pub_date="1777-10-10") books = models.Book.objects.filter(price__gt=250) print(books) return HttpResponse('<p>查找成功!</p>')
- 访问测试
__gte
__gte
大于等于,=
号后面为数字
# -*- coding: utf-8 -*- from statistics import mode from django.shortcuts import render,HttpResponse from app1_model import models def add_book(request): #books = models.Book.objects.create(title="九阴神功",price=200,publish="功夫出版社",pub_date="1777-10-10") books = models.Book.objects.filter(price__gte=300) print(books) return HttpResponse('<p>查找成功!</p>')
- 访问测试
__lt
__lt
小于,=
号后面为数字
# -*- coding: utf-8 -*- from statistics import mode from django.shortcuts import render,HttpResponse from app1_model import models def add_book(request): #books = models.Book.objects.create(title="九阴神功",price=200,publish="功夫出版社",pub_date="1777-10-10") books = models.Book.objects.filter(price__lt=300) print(books) return HttpResponse('<p>查找成功!</p>')
- 访问测试
__lte
__lte
小于等于,=
号后面为数字
# -*- coding: utf-8 -*- from statistics import mode from django.shortcuts import render,HttpResponse from app1_model import models def add_book(request): #books = models.Book.objects.create(title="九阴神功",price=200,publish="功夫出版社",pub_date="1777-10-10") books = models.Book.objects.filter(price__lte=300) print(books) return HttpResponse('<p>查找成功!</p>')
- 访问测试
__range
__range
在…之间,=
号后面为两个元素的列表
# -*- coding: utf-8 -*- from statistics import mode from django.shortcuts import render,HttpResponse from app1_model import models def add_book(request): #books = models.Book.objects.create(title="九阴神功",price=200,publish="功夫出版社",pub_date="1777-10-10") books = models.Book.objects.filter(price__range=[250,350]) print(books) return HttpResponse('<p>查找成功!</p>')
- 访问测试
__contains
__contains
包含,=
号后面为字符串
# -*- coding: utf-8 -*- from statistics import mode from django.shortcuts import render,HttpResponse from app1_model import models def add_book(request): #books = models.Book.objects.create(title="九阴神功",price=200,publish="功夫出版社",pub_date="1777-10-10") books = models.Book.objects.filter(price__contains="金庸") print(books) return HttpResponse('<p>查找成功!</p>')
- 访问测试
__icontains
__icontains
包含==(不区分大小写)==,=
号后面为字符串- 提前增加数据
- 修改文件
# -*- coding: utf-8 -*- from statistics import mode from django.shortcuts import render,HttpResponse from app1_model import models def add_book(request): #books = models.Book.objects.create(title="Python",price=500,publish="Python出版社",pub_date="1970-12-10") books = models.Book.objects.filter(title__icontains="python") print(books) return HttpResponse('<p>查找成功!</p>')
- 访问测试
__startswith
__startswith
以指定字符开头,=
号后面为字符串
# -*- coding: utf-8 -*- from statistics import mode from django.shortcuts import render,HttpResponse from app1_model import models def add_book(request): #books = models.Book.objects.create(title="Python",price=500,publish="Python出版社",pub_date="1970-12-10") books = models.Book.objects.filter(title__startswith="P") print(books) return HttpResponse('<p>查找成功!</p>')
访问测试
__endswith
__endswith
以指定字符结尾,=
号后面为字符串
# -*- coding: utf-8 -*- from statistics import mode from django.shortcuts import render,HttpResponse from app1_model import models def add_book(request): #books = models.Book.objects.create(title="Python",price=500,publish="Python出版社",pub_date="1970-12-10") books = models.Book.objects.filter(title__endswith="on") print(books) return HttpResponse('<p>查找成功!</p>')
访问测试