Django Database basic operations-additions and deletions (tip)-Basic __ Database

Source: Internet
Author: User

Original address


1. Inserting data

Python code >>> from books.models import publisher >>> P1 = publisher (name= ' Apress ', address= ' 2855 Telegr    APh Avenue ', ... city= ' Berkeley ', state_province= ' CA ', country= ' U.S.A ', ... website= ' http://www.apress.com/') >>> P1.save () >>> from books.models import publisher >>> P1 = Publisher (name= ' Apress ', add ress= ' 2855 Telegraph Avenue ', ... city= ' Berkeley ', state_province= ' CA ', country= ' U.S.A ', ... website= ' http:// www.apress.com/') >>> P1.save ()

2, inquiry

Python code >>> Publisher.objects.all () [<publisher:apress>, <publisher:o ' reilly>] >>> Pu Blisher.objects.all () [<publisher:apress>, <publisher:o ' reilly>]

To get a single object:

Python code >>> Publisher.objects.get (name= "Apress") <Publisher:Apress> >>> Publisher.objects.get (name= "Apress") <Publisher:Apress> throws an exception if the result is multiple objects or no results are returned


3, Conditions

Screening:

Python code >>> Publisher.objects.filter (name= ' Apress ') [<publisher:apress>] >>> Publisher.objects.filter (name= ' Apress ') [<publisher:apress>]
Python code >>> Publisher.objects.filter (name__contains= "Press") [<publisher:apress>] >>> Publisher.objects.filter (name__contains= "Press") [<publisher:apress>] __contains part is translated into like statements by Django

Sort:

Python Code >>> Publisher.objects.order_by ("name") [<publisher:apress>, <publisher:o ' reilly>] ;>> Publisher.objects.order_by ("name") [<publisher:apress>, <publisher:o ' reilly>]

Equivalent to ORDER by name ASC

Python Code >>> Publisher.objects.order_by ("-name") >>> Publisher.objects.order_by ("-name") plus a minus sign equals ORDER BY name Desc


Limit return Data:

Python code >>> Publisher.objects.order_by (' name ') [0] <Publisher:Apress> >>> publisher.objects . order_by (' name ') [0] <Publisher:Apress> equivalent limit 1

Python code >>> Publisher.objects.order_by (' name ') [0:2] >>> Publisher.objects.order_by (' name ') [0:2] Equivalent to OFFSET 0 LIMIT 2

4, update

Python code >>> Publisher.objects.filter (id=52). Update (name= ' Apress Publishing ') >>> Publisher.objects.filter (id=52). Update (name= ' Apress Publishing ')
Python code >>> p = Publisher.objects.get (name= ' Apress ') #先查询 >>> p.name = ' Apress Publishing ' #更新 > >> p.save () #保存 >>> p = Publisher.objects.get (name= ' Apress ') #先查询 >>> p.name = ' Apress Publishi Ng ' #更新 >>> p.save () #保存
5, delete

Python code >>> p = Publisher.objects.get (name= "O ' Reilly") >>> p.delete () >>> p = Publisher.ob Jects.get (name= "O ' Reilly") >>> P.delete ()
Python code >>> Publisher.objects.filter (country= ' USA '). Delete () >>> Publisher.objects.filter ( country= ' USA '). Delete ()

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.