昨日内容:
ORM高级查询
-filter
id=3
id__gt=3
id__lt=3
id__lte=3
id__gte=3
-in /not in
.filter(id__in=[]) in
.exclude(id__in=[]) not in
-between......and
.filter(id__range=[])
-like
.filter(name__startswith='XX') like 'XX%'
.filter(name_istartswith='XX') #忽略大小写
.filter(name__endswith='XX') like'%XX'
.filter(name__contains='XX') like'%XX%'
.filter(name_regax='^XX$') like'%XX%'精准匹配
-or
from django.db.models import Q
Q和|连用为or
&为and
-order by
models.UserInfo.objects.all().order_by('id')#升序#('-id')则为降序
#若有2个值,则第一个先排序,第二个后排序
-group by
from django.db.models import Sum,Count,Min,Max
models.UserInfo.objects.values('name').annota1te(xx=Sum('age'))
models.UserInfo.objects.values('name').annota1te(xx=Sum('age')).filter(XX__gt=50)#二次筛选
-count
models.UserInfo.objects.count()
-limit分页
models.UserInfo.objects.all()[X,XX]#利用切分来分页
-distinct
models.UserInfo.objects.values('XX').distinct()
-原生SQL
from django.db import connection
-first
-last两个返回的都是对象
"htmlcode">
id name age
1 zekai 18
2 lxxx 58
private:
id salary sp_id(外键+unique)
1 5000 1
2 4000 2
一对一
OneToOneField("表名",null=True)默认要求该字段必须是唯一的unique外键关系
#从母表查询子表的数据
查询一下name='zekai'的这条数据
res = models.UserInfo.objects.filter(name='zekai').first()
print(res.private.salary)
#res.子表表名小写.子表字段名
#从字表查询母表中的数据
查询salary=5000的这个数据所对应的用户的信息
res= models.Private.objects.filter(salary=5000).first()
print(res.sp.name,res.sp.age)
#res.关联的字段名.母表字段名
多对多
相亲模型
boy:
id name
1 laowang
2 XXX
girl:
id name
1 正正
2 连连
3 爱爱
4 凤姐
5 乔碧萝
boy2girl
id bid gid
1 1 1
2 1 2
3 1 3
4 2 2
5 2 4
6 2 5
models.py:
class Boy(models.Model):
bname=models.CharField(max_length=32,null=True)
"Boy",null=True)
g = models.ForeignKey("Girl",null=True)
"htmlcode">
from django.core.paginator import Paginator
#per_page:每页显示条目数量
#count:数据总个数
#num_pages:总页数
#page_range:总页数的索引范围
#page:page对象
paginator = Paginator(userlist,10)
#has_next:是否有下一页
#next_page_number:下一页页码
#has_previous:是否有上一页
#previous_page_number:上一页页码
#object_list:分页之后的数据列表
#number:当前页
#paginator:paginator对象
sers = paginator.page(cur_page)
return render(request,'index.html',{"users":users})
"<li><a href='#' aria-label='Previous'><span aria-hidden='true'>«</span></a></li>"
else:
astr = "<li><a href='/custom/" % (self.cur_page-1)
page_list.append(astr)
"<a style='display:inline-block; padding:5px;margin:5px;background-color:red;' href='/custom/" % (i, i)
astr = "<li class='active'><a href='/custom/" % (i, i)
else:
# astr = "<a style='display:inline-block; padding:5px;margin:5px' href='/custom/" % (i, i)
astr = "<li><a href='/custom/" % (i, i)
page_list.append(astr)
"<li><a href='#' aria-label='Next'><span aria-hidden='true'>»</span></a></li>"
else:
astr = "<li><a href='/custom/" % (self.cur_page+1)
page_list.append(astr)
" ".join(page_list)
"custom.html", {"user_list":user_list, "page":page})
安全攻击
- -XSS
- -csrf
- -sql注入
以上就是本次介绍的全部相关知识点,感谢大家对的支持。
标签:
djano,分页
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
白云城资源网 Copyright www.dyhadc.com
暂无“djano一对一、多对多、分页实例代码”评论...
更新日志
2025年10月27日
2025年10月27日
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]