Send data in ajax django query optimization and encoding format summary

orm query optimization

1)only与refer

only method returns a queryset object is essentially a list of sets of data objects

The object is only contained within brackets only the specified property (other properties can also be obtained, but the need to re-take the database query)

defer to each other and only anti-relationship, returns a queryset object is essentially a list of sets of data objects; the object contains only addition to defer brackets within the specified attribute (in brackets is also available but need to take the database)

2)select_related与prefetch_related

Select_related only put in parentheses foreign key fields, and the type of the foreign key field is only one or one to many, operation of the internal table is associated, it will be a foreign key table with the current tables directly spliced ​​together, and then perform query, the returned result is a QuerySet, sets the list data object, the data object the data acquisition or data table associated with the current table, the database will not walk;

prefetch_related brackets inside and outside the key field all the support, the interior is sub-query results returned is a queryset objects, a list of sets of data objects, the data object acquiring data in the current table or related tables, the database will not walk;

The first method is mainly consumed in the associated time-consuming operation table, a second method takes the number of queries in the main consuming;

choices field

In some fields with data that can be clearly list all possible; such as: gender, work experience, education, marital status, source of customers and so on;

1. First definition of a good correspondence relationship;. 2 choices parameters specified relationship field.

= gender_choices (
(. 1, 'MALE'),
(2, 'FEMALE'),
(. 3, 'Others'),
)
Gender = models.IntegerField (choices = gender_choices)
if you pre-defined data range inside, you can get through the interpretation information corresponding to the field name get_ _display ().

ajax

  1. 2. Asynchronous submit partial refresh

The basic syntax structure ajax
active is based JQuery ajax packaged, so using ajax when the first introducing the jQuery;

$.ajax({
    url:'', # 后端的地址,特性跟action一直,三种情况
    type:'post', # 请求方式,小写
    data:{'username':'zhang',password:'123'}, # 提交的数据
    success:function(data){ # data异步提交的结果
        # 回调机制返回的结果
        # window.location.href = url
    }
})
#基于ajax做数据交互,后端无论返回什么结果都会被回调函数捕获,不会再影响整个页面

Front and rear ends data transmission encoding format

urlencoded
    数据格式username=zhang&password=123
    django针对符合urlencoded编码格式的数据,会自动解析并放到request.POST中;
    
formdata
    form表单发送文件必须要指定的编码格式
    该编码格式既可以发文件也可以发普通的键值对
    
    django后端自动识别,将内部符合urlencoded编码格式的数据,自动解析并将文件类型的数据解析封装到request.FILES中
    
application/json
    ajax可以发送json格式的数据,form表单不支持
#注意:数据类型和编码格式要保证一致性

How ajax json format to send data

  1. Need to add a parameter in the front
    contentType: 'application / json'

  2. We need to serialize the data format string json

    JSON.stringfy ({ 'username': ' zhang', 'password': '123'})
    Note: django backend json format for the data, does not do any processing, in place of the intact request.body

ajax to send the file (you can not just send the file, you can also send an ordinary key-value pairs)

Suggestions to help you do the data carried by means of built-in objects native js
1) Mr. into a built-in object.
Var MyFormData = new new FormData ();

2) Then add the data within the object toward (and ordinary documents can be key-value pair)

#普通键值对
MyFormData.append('name','value')
MyFormData.append('name1','value1')
MyFormData.append('name2','value2')

Data file #
How to obtain the input data file label file
var MyFileobj = $ ( "input [ type = 'file']") [0] .files [0];

3. The need for additional two parameters
contentType: false, # coding does not specify any object can be identified with encode django;
the processData: to false, # browser does not need any data processing

#django common key can be identified on the object and the object file, and then were placed in different ways and in FILES POST;

Guess you like

Origin www.cnblogs.com/zhangchaocoming/p/11972843.html