First, query table records
1. Query the relevant API
Now the main query API usage examples are described below, where student is the defined table class:
(1). Student.objects.all () #返回的QuerySet类型 query all records [Obj1,obj2 ....] (2). Student.objects.filter () #返回的QuerySet类型 query all Eligible Records (3). Student.objects.exclude () #返回的QuerySet类型 query all non-qualifying records, outside of the filter criteria (4). Student.objects.get () #返回的models对象 query result must have only one, otherwise error (5). Student.objects.all (). First () #返回的models对象 in the query results collection, the default is to sort by primary key (6). Student.objects.filter (). Last () #返回的models对象 the final of the query results collection by default, sorted by primary key (7). Student.objects.all (). VALUES ("name", "class_id") #返回的QuerySet类型, the dictionary (8) that is stored in the list. Student.objects.all (). Values_list ("name", "class_id") #返回的QuerySet类型, a tuple (9) that is stored in the list. Student.objects.all (). order_by ("class_id") #按指定字段排序, not specified, sorted by primary key (10). Student.objects.all (). Count () #返回的记录个数 (11). Student.objects.all (). VALUES ("name"). Distinct () #返回记录进行去重 (12). Student.objects.all (). exist () #查询结果是否存在, returns TRUE or False
Filter ():
In the following example, the query results are of type Queryset, the filter condition in parentheses, and the relationships with multiple conditions.
Stu_list=student.objects.filter (name= "Longhua", class_id=6)
Exclude ():
In the following example, the query result is of type queryset, and the result is data other than the filter in parentheses.
Stu_list=student.objects.exclude (name= "Alex")
First ():
In the following example, the query result is the models type, which is the first result of sorting by primary key.
#stu_obj =student.objects.all (). First () Stu_obj=student.objects.filter (birth__year= "). First () print (stu_ Obj.name)
Order_by ():
In the following example, the query result is of type Queryset and the query results are sorted.
Stu_list=student.objects.all (). order_by ("-class_id") #降序排列stu_list =student.objects.all (). order_by ("-class_ ID ") #升序排列
VALUES () differs from value_list ():
Ret=book.objects.values_list ("title") #values_list ("title") before adding all () can also add filter () print (ret) #< QuerySet [(' Prince and Frog ',), (' The memory of the Princess '), (' Love and Hate '),]>ret=book.objects.values ("title") #values "title") All () can also be added filter () print (ret) #<queryset [{' title ': ' Prince and Frog '}, {' title ': ' Princess Memory '}, {' title ': ' Love and Hate '}]>
Get ():
The data that the filter is pointing to must have only one record, otherwise an error, and the result is the model type.
exist ():
When there is data exist, the following two ways can be executed if statements, but from the execution of the SQL statement, we can see the meaning of the existence of exist, he will only query a record, which is a large number of data tables, query efficiency is very high.
#方式一: Stu_list=student.objects.all (). Exists () #翻译的sql语句: Select (1) as "a" from "App01_student" LIMIT 1; args= () if Stu_list: print ("OK") #方式二: Stu_list=student.objects.all () #翻译的sql语句: Select "App01_student". " Tid "," app01_student "." Name "," App01_student "." Birth "," app01_student "." "From" App01_student "if Stu_list: print (" OK ")
2. Double Underline single Table query
The details are as follows:
Python Day74 ORM II