table of Contents
ORM query optimization
only与defer
Fields put the query results within brackets is a list of only one set of data objects within the data field properties of these objects point brackets will no longer query the database directly is the object of obtaining property also some field support click-per-click inside the brackets but it will once again take the database
defer to each other only is the inverse operation of
what to put in the field defer parentheses check out the object can not attribute this field if you want to click on each click is necessary to re-take once you click on a data field in the non-brackets will not just take the database operation object attributes
select_related与prefetch_related
Only put foreign key fields and data type within parentheses select_related foreign key field is not only one-to-many or many to many
Internal table is a table operation will automatically connect to the inner and outer brackets key field associated with the current table to an automatic splicing table and a query the data table out of the package into one object
The advantage of this is that the table does not need to re-take across the database to reduce the pressure on the database
Can put a plurality of commas within the foreign key field associated with a plurality of spaced brackets select_related will table with the foreign key fields makes up a current list of all
Internal perfetch_related subquery
It will automatically help you step by step query multiple tables and the results of the query into an object package
Feel to the user as if the operation was linked table
Support for multiple foreign key field pass in parentheses and no restrictions on the type
features: each put a foreign key field will be more and more to take a sql statement to query a table
Time: queries
parameter choices
Users Table
gender
-service state
education
marital status
...
These possibilities are our top field full list of
saved numbers to take the time in advance in accordance with
The first parameter is not necessarily a number or a string
As long as you are
MTV and MVC model
django claiming to nature but it is still MVC framework MTV
MTV
M: models
T: templates
V: views
The MVC
M: Models
V: views
C: route matches controllar
Introduction to Ajax
XML is a markup language
the grammar scenarios:
1. Write the profile
2. You can write the front page (in erp odoo framework)
Each company will have a unique part of the company's internal management software
specially to develop internal management software framework odoo
odoo framework to achieve all internal functions dependent on python2
payroll calculation
The biggest advantage is AJAX without reloading the entire page, you can exchange data with the server and update parts of the page content. (This feature is the feeling to the user request and response process is completed unknowingly)
Requirements:
There are three input box on the page
before the last two numbers show both digital and
requirements page does not refresh
When we learn only learn to master the method does not require JQuery packaged native js version
when in use must first import jQuery
in brackets remember to manually enter a brace
$.ajax({
})
ajax basic grammatical structures
$.ajax({
url:'', // 数据提交的后端地址 不写就是往当前页面提交 也可以写后缀 也可以写全称 跟actions一样
type:'post', // 提交方式 默认是get请求
data:{'i1':$('#d1').val(),'i2':$('#d2').val()}, // 提交的数据
success:function (data) { // 形参data就是异步提交之后后端返回结果
$('#d3').val(data) // 回调机制需要做的事情
}
}) # 一旦你使用了ajax 必知必会三板斧都不再作用与页面 而是与data交互
href tag parameter a get request
form Form get / POST
Ajax get / POST
get request data format
url?xxx=ooo&zzz=yyy
Front and rear ends of data transmission
Front and rear ends the transmission data encoding format:
The front and rear ends of the interaction is a data encoding format for different data are handled differently backend
request.POST
request.FILES
You only need to know three kinds
1.urlencoded
2.formdata
3.application/json
transmitting the encoded form form data format
The Type-the Content: file application / X-WWW-form-urlencoded
1.form form default encoding is urlencoded
urlencoded corresponding data format
username = password = 123 & Jason
Django automatically resolves against the rear end of the data and help you urlencoded encapsulated request. the POST
2.form form to send the file encoding format Content-Type: multipart / formdata; boundary = ---- WebKitFormBoundaryhjKCHQHDmcE62iMQ
for formdata data format in the browser you are unable to view
As long as you django backend data format satisfies urlencoded
username = jason & password = 123
will automatically help you to resolve the request.POST
If you are a rear django file object will automatically identify help you put request.FILES in
3.form form can not send data json format you want to think you can only by means of ajax
transmitting the encoded data format ajax
ajax can transmit data in three formats
1.urlencoded
2.formdata
3.Application / JSON
The Type-the Content: file application / X-WWW-form-urlencoded; charset = UTF. 8-
Ajax default encoding format is urlencoded means django backend data is parsed into the request.POST
When it comes to interaction before and after the end you have to be consistent with the format of the encoded data format
Is a data format of the request header is another format
How Ajax json format data transmission
dumps stringify
loads parse
Content-Type: application/json
{"username":"jason","password":"123"}
django backend data for json format will not do any processing of data is how to put request.body the only intact
requires you to manually process
$('#d1').click(function () {
$.ajax({
url:'',
type:'post',
contentType:'application/json', # 1.注意点1
data:JSON.stringify({'username':'jason','password':'123'}), # 2.注意点2
success:function (data) {
alert(123)
}
})
})
How Ajax transfer file data (*********)
New built-in objects by means of
the object which can carry data files also support common key-value pairs
$('#d1').click(function () {
// 先生成一个内置对象
var MyFormData = new FormData();
// 1. 先添加普通的键值
MyFormData.append('username','jason'); // 添加了一组普通的简直对
MyFormData.append('password','123');
// 2. 添加文件数据
MyFormData.append('myfile',$('#d2')[0].files[0]); // 如何获取input框中文件对象$('#d1')[0].files[0]
$.ajax({
url:'',
type:'post',
data:MyFormData, # 1
// 发送文件必须要指定的两个参数
contentType:false, // 不适用任何编码 MyFormData对象内部自带编码 django后端能够识别 # 2
processData:false, // 不要处理数据 # 3
success:function (data) {
}
})
})
Serialization
django built-in module serializers
drf django restframework
from app01 import models
from django.core import serializers
# 序列化目的 将数据整合成一个大字典形式 方便数据的交互
def zzz(request):
user_queryset = models.User.objects.all()
# [{username:...,password:...,hobby:...,},{},{},{}]
# user_list = []
# for data in user_queryset:
# user_list.append(
# {'username':data.username,
# 'password':data.password,
# 'gender':data.get_gender_display(),
#
# }
# )
res = serializers.serialize('json',user_queryset)
return HttpResponse(res)