Django API operations common in MySQL statements

Source: Internet
Author: User

Background

Increase/delete/change/check How to operate the database

Model for importing databases

In [23°c]: from blog.models import people

"Add record"--Three ways

Method One: Directly using the CREATE function of the data-mode object

In []: People.objects.create (name= ' Shuaige ')

OUT[24]: <people:people object>


Note: Create a successful return object address

Method Two:

In [6]: Record = people (Name= ' Choubaguai ") in [7]: Record.save ()


Method Three:

In [9]: Record = people () in [ten]: Record.name = ' Andy ' in [All]: Record.save ()


"Delete Record"--Find the object and delete it using delete ()


First find the collection of objects you want to delete (id greater than 2)

in [+]: R_d = People.objects.filter (id__gt =2)

Use the Delete object to delete it

In []: R_d.delete () out[20]: (3, {' Blog. People ': 3})


Number of items that will return coral



"Change Record"-Find it first, re-assign the object property

(Change id=1 's name to Andyliu)

in [+]: People.objects.values_list () out[21]: [(1, ' Liudehua '), (2, ' zhangxueyou ')]in []: R_m = People.objects.get (id= 1) in [all]: R_m.name = ' Andyliu ' in [+]: R_m.save () in [+]: People.objects.values_list () out[25]: [(1, ' Andyliu '), (2, ' en Angxueyou ')]





"Check" This is equivalent to the SQL statement in MySQL in the SELECT statement, where there are many word statements to limit the query


1 Get all Record objects

All_record = People.objects.all ()

If you want to get an object value, you need to take an object out first and then remove the property from the object

in [+]: for record in All_record: ....: print (Record.id) ....: Print (Record.name) ....: 1andyliu2zhangx Ueyou



2 Gets the object with the specified value for a field

In [all]: R_p = People.objects.get (name= ' zhangxueyou ') in []: r_p.idout[12]: 2


Note: This usage is not commonly used, if you want to use, you must ensure that you find a certain existence of such a record, and must be unique, otherwise it will be error OH



3 conditional queries similar to where in MySQL use the filter function


First: Range comparison query for integers ()

Find an object with an ID greater than 1 and return it as a list of values

In []: People.objects.filter (id__gt=1). Values_list () out[15]: [(2, ' zhangxueyou '), (6, ' Andyliu ')]



Gt
Greater than
Gte Greater than or equal
Lt Less than
Lte
Less than or equal
Ne
Not equal to
In (can also be other data types)
In the given list: People.objects.filter (Id__in=[1, 3, 4]) returns a list of polls (ID values are 1 or 3 or 4, respectively).
in [+]: People.objects.filter (name__in=[' Andyliu ', ' zhangxueyou ']) out[16]: [<people:people object>, < People:people object> <people:people Object>]



4 Multi-filter query, one way is to use the filter function multiple times to

in [+]: People.objects.filter (id__gt=1). Filter (name= ' Andyliu ') out[17]: [<people:people object>]in [18]: People.objects.filter (id__gt=1). Filter (name= ' Andyliku ') out[18]: []


5 Object Data found (that is, the number of records that meet the criteria)


in [+]: People.objects.count ()

OUT[25]: 3

Equals:

in [+]: People.objects.all (). Count ()

OUT[26]: 3


The length of the meeting condition

in [+]: People.objects.filter (name= ' Andyliu '). Count () out[24]: 2


6 order_by () function, ascending/Descending


-negative indicates that you want to sort in descending order

in [+]: p = People.objects.order_by ('-id ') in [all]: For I in P:print (i.id) ....: 621


7 haha next may be a query about time (to be continued)

This article is from the "Tridewah operation and maintenance work Road" blog, please be sure to keep this source http://cuidehua.blog.51cto.com/5449828/1776023

Django API operations common in MySQL statements

Related Article

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.