The database additions and deletions of the ORM in Django

Source: Internet
Author: User

What is ORM? ORM (Object-relational mapping) is an object-oriented approach to the process of creating tables in a database and adding additions and deletions to data.

Simply put, a table in a database is treated as a class, and each record in the database is treated as an object. Defining a class in Django is creating a table in the database. The object that instantiates a class in Django is the addition of a record to the database. Deleting an object in Django is the deletion of a record in the database. Changing the properties of an object in Django is the value of modifying a record in the database. Traversing the property values of the Query object in Django is querying the values of the records in the database.

Here are a few of the command statements in the Django Views view function.

One, increase (Create,save)

From app01.models Import *

#create方式一: Author.objects.create (name= ' Alvin ')

#create方式二: Author.objects.create (**{"name": "Alex"})

#save方式一: Author=author (name= "Alvin")
Author.save ()

#save方式二: Author=author ()
Author.name= "Alvin"
Author.save ()

Note that the increase in Create+save two combinations, create is the increase of the action, save is added action, missing one is not in.

Second, delete (delete)

>>> Book.objects.filter (id=1). Delete () (3, {'app01. Book_authors'app01. Book': 1})

 If it is a many-to-many relationship: the Remove () and Clear () methods:

#正向
Book = models. Book.objects.filter (id=1)

#删除第三张表中和女孩1关联的所有关联信息
Book.author.clear () #清空与book中id all data associated with =1
Book.author.remove (2) #可以为id
Book.author.remove (*[1,2,3,4]) #可以为列表, Front plus *

#反向
Author = models. Author.objects.filter (id=1)
Author.book_set.clear () #清空与boy中id all data associated with =1


Third, change (update and save)
#----------------The Update method to set the corresponding property directly----------------
Models. Book.objects.filter (id=3). Update (title= "PHP")
# #sql:
# #UPDATE "App01_book" SET "title" = ' PHP ' WHERE ' App01_book '. " id "= 3; args= (' PHP ', 3)


#---------------The Save method will reset all properties again, inefficient-----------
Obj=models. Book.objects.filter (id=3) [0]
Obj.title= "Python"
Obj.save ()
# select "App01_book". " ID "," App01_book "." Title "," App01_book "." Price ",
# "App01_book". " Color "," App01_book "." Page_num ",
# "App01_book". " publisher_id "from" App01_book "WHERE" App01_book "." id "= 3 LIMIT 1;
#
# UPDATE "App01_book" SET "title" = ' Python ', "price" = 3333, "color" = ' red ', "page_num" = 556,
# "publisher_id" = 1 WHERE "App01_book". " id "= 3;














The database additions and deletions of the ORM in Django

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.