博客网站开发报告,网站卡密代理怎么做,wordpress 搜索强化,郑州黑马程序员培训机构官网多表操作
1 基于对象的跨表查
子查询----》执行了两句sql#xff0c;没有连表操作 2 基于双下滑线的连表查
一次查询#xff0c;连表操作
3 正向和反向
放在ForeignKey,OneToOneField,ManyToManyField的-related_namebooks#xff1a;双下滑线连表查询#xff0c;反向…多表操作
1 基于对象的跨表查
子查询----》执行了两句sql没有连表操作 2 基于双下滑线的连表查
一次查询连表操作
3 正向和反向
放在ForeignKey,OneToOneField,ManyToManyField的-related_namebooks双下滑线连表查询反向查询按表名小写---》用来替换表名小写publish__books__name-related_query_namebooks:基于对象跨表查反向查询---》用来替换表名小写pubilsh.book_set.all()pubilsh.books.all()
4聚合查询
aggregate是 QuerySet 的一个终止子句用来做聚合查询聚合函数Avg,Count,Min,Max,Sum -使用select name,price,avg(price) as price__avg from book; Book.objects.all().aggregate(Avg(price))select name,price,avg(price) as average_price from book; Book.objects.aggregate(average_priceAvg(price))ret Book.objects.all().aggregate(avg_priceAvg(price), min_priceMin(price))
5 分组查询--》分组后通常会用聚合---》annotate用来分组和聚合的
annotate filter在annotate前表示过滤where条件values在annotate前表示分组的字段如果不写表示按整个表分组filter在annotate后表示 having条件values在annotate后表示取字段---》只能取分组字段和聚合函数字段分组的目的把有相同特征的分成一组分成一组后一般用来统计总条数统计平均数求最大值 -统计每一本书作者个数---》 Book.objects.all().values(id).annotate(author_numCount(authors)).values(name,author_num)-统计每一个出版社的最便宜的书---》按出版社Publish.objects.all().valuse(id).annotate(min_priceMin(book__price)).vlaues(name,min_price)Publish.objects.annotate(MinPriceMin(book__price))-查询每一个书籍的名称以及对应的作者个数--按书分Book.objects.all().values(id).annotate(count_publishCount(authors)).value(name,count_publish)-查询每一个以 红开头 书籍的名称以及对应的作者个数--按书分Book.objects.all().filter(name__startswith红)values(id).annotate(count_publishCount(authors)).value(name,count_publish)-查询每一个以 红开头 书籍的名称以及对应的作者个数大于3的记录--按书分
Book.objects.all().filter(name__startswith红)values(id).annotate(count_publishCount(authors)).filter(count_publish__gt3).value(name,count_publish)
6 F查询与Q查询
F查询拿到某个字段在表中具体的值 -查询评论数大于收藏数的书籍from django.db.models import FBook.objects.filter(评论数__gtF(收藏数))-让所有图书价格 1Book.objects.all().update(priceF(price)1) Q查询为了组装成 与 或 非 条件 -与条件and条件在filter中直接写---》就是and条件Book.objects.filter(authors__namelqz,price100)-或条件Book.objects.filter(Q(authors__namelqz)|Q(authors__namejustin))-非条件Book.objects.filter(~Q(name红楼梦))-复杂逻辑(名字为红楼梦并且价格大于100) 或者 id 大于 2Book.objects.filter((Q(name红楼梦) Q(price__gt100))|Q(nid__gt2))
其他字段和字段参数 字段参数ORM字段参数
null用于表示某个字段可以为空。unique 如果设置为uniqueTrue 则该字段在此表中必须是唯一的 。db_index如果db_indexTrue 则代表着为此字段设置索引。default为该字段设置默认值。DateField和DateTimeField auto_now_addTrue新增会把当前时间存入defaultdatatime.datatime.nowauto_nowTrue每次更新数据记录的时候会更新该字段verbose_name 提示,该字段的作用blank Admin中是否允许用户输入为空editable Admin中是否可以编辑help_text Admin中该字段的提示信息choices Admin中显示选择框的内容用不变动的数据放在内存中从而避免跨表操作 get_字段名_display()
ForeignKey 属性 to设置要关联的表 to_field 设置要关联的表的字段 related_name 反向操作时使用的字段名用于代替原反向查询时的’表名_set’。 related_query_name 反向查询操作时使用的连接前缀用于替换表名。 on_delete 当删除关联表中的数据时当前表与其关联的行的行为。 models.CASCADE删除关联数据与之关联也删除 models.DO_NOTHING 删除关联数据引发错误IntegrityError models.PROTECT 删除关联数据引发错误ProtectedError models.SET_NULL删除关联数据与之关联的值设置为null前提FK字段需要设置为可空 models.SET_DEFAULT删除关联数据与之关联的值设置为默认值前提FK字段需要设置默认值 models.SET 删除关联数据 a. 与之关联的值设置为指定值设置models.SET(值) b. 与之关联的值设置为可执行对象的返回值设置models.SET(可执行对象) db_constraint---》公司一般都设置为False 是否在数据库中创建外键约束默认为True db_constraintFalse 在数据库中不建立外键约束 虽然不建立数据库外键约束---》但是orm查询继续用 ManyToManyField
用于表示多对多的关联关系。在数据库中通过第三张表来建立关联关系 to 设置要关联的表中间是有个中间表的区别于一对多 related_name 同ForeignKey字段。 related_query_name 同ForeignKey字段。 through 在使用ManyToManyField字段时Django将自动生成一张表来管理多对多的关联关系。 但我们也可以手动创建第三张表来管理多对多关系此时就需要通过through来指定第三张表的表名。 through_fields设置关联的字段。 db_table 默认创建第三张表时数据库中表的名称。 中间表创建方式
自动生成用不到through 和 through_fields authorsmodels.ManyToManyField(toAuthor,db_table中间表表名) 自动创建中间表有快捷操作 addremovesetclear book表id name price1 西游记 222 红楼梦 33bookToauthorsid book_id author_id 1 1 12 1 2author表id name gender age1 lqz 男 182 罗贯中 女 22
手动创建中间表使用through指定 三张表都要手动创建--》3个类--》3个表模型---》 什么情况会使用手动创建----中间表如果有多的字段都是手动创建 authorsmodels.ManyToManyField(toAuthor,throughbooktoauthor, through_fields(当前表--》到中间表的外键关系剩下的写在第二个位置)) book表id name price1 西游记 222 红楼梦 33booktoauthorid book_id author_id 日期1 1 1 2 1 2author表id name gender age1 lqz 男 182 罗贯中 女 22
纯手动创建中间表不使用ManyToManyField关联 不会在book或author表中加 ManyToManyField 字段了 book表id name price1 西游记 222 红楼梦 33booktoauthorid book_id author_id 日期1 1 1 2 1 2author表id name gender age1 lqz 男 182 罗贯中 女 22
在表中都可以定义要给内部类
class Author(models.Model):name models.CharField(max_length32)class Meta: #元信息db_tableindex_togetherunique_togetherordering # 默认按id排序
django与ajax
ajax:异步Javascript和XML
作用Javascript语言与服务器(django)进行异步交互传输的数据为XML当然传输的数据不只是XML,现在更多使用json数据
同步交互异步交互 同步交互js发送出请求---》直到请求回来---》页面不能操作不能点击 异步交互js发出请求---》等待请求回来的过程中---页面可以随意继续操作 使用使用了jq帮咱们封装的方法 ajax 名字跟ajax相同 $.ajax
真正的ajax原生需要使用js操作jq的ajax方法是对原生js的封装方便咱们使用 -前后端混合项目中我们通常使用jq的ajax实现 js和后端异步交互 -jq操作dom -jq发ajax请求 -前后端分离项目中我们会使用另一个第三方库实现 js和后端异步交互axios -只想发送ajax请求---》只用来发ajax请求的库 计算 小案例
demo01.html
!DOCTYPE html
html langen
headmeta charsetUTF-8titleTitle/titlescript srchttps://cdn.bootcdn.net/ajax/libs/jquery/3.7.1/jquery.min.js/scriptlink hrefhttps://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css relstylesheetscript srchttps://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js/script
/head
bodyh1写一个计算小案例--ajax/h1
input typetextnameone idone input typetextnametwo idtwo input typetextnamethreeidthree
button id id_btn计算/button
/body
script$(#id_btn).click(function (){{#alert(xxx)#}var one$(#one).val()var two$(#two).val()$.ajax({url:/demo01/,method:post,data:{one,two},success:function (res){console.log(typeof res)if (res.code100){$(#three).val(res.result)}else {alert(res.msg)}}})})
/script
/html views.py
def demo01(requset):if requset.methodGET:return render(requset,demo01.html)else:oneint(requset.POST.get(one))twoint(requset.POST.get(two))return JsonResponse({code:100,msg:计算成功,result:onetwo})上传文件
demo01.html
!DOCTYPE html
html langen
headmeta charsetUTF-8titleTitle/titlescript srchttps://cdn.bootcdn.net/ajax/libs/jquery/3.7.1/jquery.min.js/scriptlink hrefhttps://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css relstylesheetscript srchttps://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js/script
/head
bodyhr
h1文件上传/h1
input typefile idid_filebutton idid_submit文件上传/button
/body
script$(#id_submit).click(function (){{#alert(xxx)#}var formdata new FormData()//$(#id_file)[0].files[0]//$(#id_file)根据id拿到标签———》jq把标签放到一个列表中//取第0个位置是取出第一个符合条件【id为id_file】的标签想拿文件---》标签对象.files---对象---》从对象中取出key为0队友的文件对象formdata.append(myfile,$(#id_file)[0].files[0])$.ajax({url:/demo01/,method:post,//指定编码上传文件processData: false,contentDocument:false,data:formdata,success:function (res){if (res.code100){alert(res.msg)}else {alert(res.msg)}}})})
/script
/html
views.py
def demo01(requset):if requset.methodGET:return render(requset,demo01.html)else:myfilerequset.FILES.get(myfile)with open(myfile.name,wb) as f:for line in myfile:f.write(line)return JsonResponse({code:100,msg:文件上传成功,}) json格式用的多后期 $.ajax({url: /demo01/,method: post,contentType: application/json,data: JSON.stringify({name: lqz, age: 19}), // 把对象转成字符串形式,json格式字符串success: function (data) {console.log(data)}}) 文章转载自: http://www.morning.ffdyy.cn.gov.cn.ffdyy.cn http://www.morning.pqwrg.cn.gov.cn.pqwrg.cn http://www.morning.ktnt.cn.gov.cn.ktnt.cn http://www.morning.rfhwc.cn.gov.cn.rfhwc.cn http://www.morning.srwny.cn.gov.cn.srwny.cn http://www.morning.mlffg.cn.gov.cn.mlffg.cn http://www.morning.txfxy.cn.gov.cn.txfxy.cn http://www.morning.nlygm.cn.gov.cn.nlygm.cn http://www.morning.bpyps.cn.gov.cn.bpyps.cn http://www.morning.ljdd.cn.gov.cn.ljdd.cn http://www.morning.znlhc.cn.gov.cn.znlhc.cn http://www.morning.qmbtn.cn.gov.cn.qmbtn.cn http://www.morning.hnk25076he.cn.gov.cn.hnk25076he.cn http://www.morning.pswzc.cn.gov.cn.pswzc.cn http://www.morning.sqqhd.cn.gov.cn.sqqhd.cn http://www.morning.xrct.cn.gov.cn.xrct.cn http://www.morning.yaqi6.com.gov.cn.yaqi6.com http://www.morning.ldqzz.cn.gov.cn.ldqzz.cn http://www.morning.iknty.cn.gov.cn.iknty.cn http://www.morning.aswev.com.gov.cn.aswev.com http://www.morning.tklqs.cn.gov.cn.tklqs.cn http://www.morning.ltksw.cn.gov.cn.ltksw.cn http://www.morning.rcttz.cn.gov.cn.rcttz.cn http://www.morning.qmbgb.cn.gov.cn.qmbgb.cn http://www.morning.wqnc.cn.gov.cn.wqnc.cn http://www.morning.qlsyf.cn.gov.cn.qlsyf.cn http://www.morning.qnkqk.cn.gov.cn.qnkqk.cn http://www.morning.dcpbk.cn.gov.cn.dcpbk.cn http://www.morning.lsxabc.com.gov.cn.lsxabc.com http://www.morning.lqynj.cn.gov.cn.lqynj.cn http://www.morning.wbrf.cn.gov.cn.wbrf.cn http://www.morning.qpxrr.cn.gov.cn.qpxrr.cn http://www.morning.tcxk.cn.gov.cn.tcxk.cn http://www.morning.tqfnf.cn.gov.cn.tqfnf.cn http://www.morning.xbmwm.cn.gov.cn.xbmwm.cn http://www.morning.fwrr.cn.gov.cn.fwrr.cn http://www.morning.ykgkh.cn.gov.cn.ykgkh.cn http://www.morning.duqianw.com.gov.cn.duqianw.com http://www.morning.tndxg.cn.gov.cn.tndxg.cn http://www.morning.lstmq.cn.gov.cn.lstmq.cn http://www.morning.rltw.cn.gov.cn.rltw.cn http://www.morning.jqsyp.cn.gov.cn.jqsyp.cn http://www.morning.wdxr.cn.gov.cn.wdxr.cn http://www.morning.jxhlx.cn.gov.cn.jxhlx.cn http://www.morning.gwjqq.cn.gov.cn.gwjqq.cn http://www.morning.jstggt.cn.gov.cn.jstggt.cn http://www.morning.prgyd.cn.gov.cn.prgyd.cn http://www.morning.rptdz.cn.gov.cn.rptdz.cn http://www.morning.npgwb.cn.gov.cn.npgwb.cn http://www.morning.rntyn.cn.gov.cn.rntyn.cn http://www.morning.xpqdf.cn.gov.cn.xpqdf.cn http://www.morning.fqljq.cn.gov.cn.fqljq.cn http://www.morning.mbbgk.com.gov.cn.mbbgk.com http://www.morning.ybnps.cn.gov.cn.ybnps.cn http://www.morning.pmwhj.cn.gov.cn.pmwhj.cn http://www.morning.cknws.cn.gov.cn.cknws.cn http://www.morning.zqzhd.cn.gov.cn.zqzhd.cn http://www.morning.dhyqg.cn.gov.cn.dhyqg.cn http://www.morning.rrjzp.cn.gov.cn.rrjzp.cn http://www.morning.bpttm.cn.gov.cn.bpttm.cn http://www.morning.rlzxr.cn.gov.cn.rlzxr.cn http://www.morning.rckmz.cn.gov.cn.rckmz.cn http://www.morning.zqzhd.cn.gov.cn.zqzhd.cn http://www.morning.sqnrz.cn.gov.cn.sqnrz.cn http://www.morning.joinyun.com.gov.cn.joinyun.com http://www.morning.cltrx.cn.gov.cn.cltrx.cn http://www.morning.jbtzx.cn.gov.cn.jbtzx.cn http://www.morning.jhwqp.cn.gov.cn.jhwqp.cn http://www.morning.pflpb.cn.gov.cn.pflpb.cn http://www.morning.fgxws.cn.gov.cn.fgxws.cn http://www.morning.lxkhx.cn.gov.cn.lxkhx.cn http://www.morning.hnpkr.cn.gov.cn.hnpkr.cn http://www.morning.fgsct.cn.gov.cn.fgsct.cn http://www.morning.gychx.cn.gov.cn.gychx.cn http://www.morning.yksf.cn.gov.cn.yksf.cn http://www.morning.lxlfr.cn.gov.cn.lxlfr.cn http://www.morning.nqrlz.cn.gov.cn.nqrlz.cn http://www.morning.spxsm.cn.gov.cn.spxsm.cn http://www.morning.c7491.cn.gov.cn.c7491.cn http://www.morning.kgkph.cn.gov.cn.kgkph.cn