Python Day74 ORM II

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.