Django Database basic Operations-Delete and change (tip)-Basic

Source: Internet
Author: User

1. Inserting data

1 Python code2>>> fromBooks.modelsImportPublisher3>>> P1 = Publisher (name='Apress', address='2855 Telegraph Avenue',  4... city='Berkeley', state_province='CA', country='U.S.A.',  5... website='http://www.apress.com/')  6>>> P1.save ()
1>>> fromBooks.modelsImportPublisher2>>> P1 = Publisher (name='Apress', address='2855 Telegraph Avenue',  3... city='Berkeley', state_province='CA', country='U.S.A.',  4... website='http://www.apress.com/')  5>>> P1.save ()

2. Enquiry

1 Python code 2 >>> Publisher.objects.all ()  3 [<publisher:apress>, <publisher:o ' Reilly>]  
1 [python] view plain copy 2 >>> Publisher.objects.all ()  3 [<publisher:apress>, < Publisher:o'reilly>]  

To get a single object:

1 Python code 2 >>> Publisher.objects.get (name="Apress")  3 <Publisher:Apress>  
1 [python] view plain copy 2 >>> Publisher.objects.get (name="Apress")   3 <Publisher:Apress>  

Throws an exception if the result is multiple objects or no results are returned

3. Conditions

Screening:

1 Python code 2 >>> Publisher.objects.filter (name='Apress')  3 
1 >>> Publisher.objects.filter (name='Apress')   2 [<publisher:apress>]  

1 Python code 2 >>> Publisher.objects.filter (name__contains="Press")   3 [<publisher:apress>]  
1 [python] view plain copy 2 >>> Publisher.objects.filter (name__contains="Press")   3

The __contains part will be translated into a like statement by Django

Sort:

1 Python code 2 >>> Publisher.objects.order_by ("name")  3 [<publisher:apress>, <publisher:o'reilly>

1 python] View plain copy2 >>> Publisher.objects.order_by ("name"  )  3 [<publisher:apress>, <publisher:o'reilly>]  

Equivalent to ORDER by name ASC

1 Python code 2 >>> Publisher.objects.order_by ("-name")  
1 [python] view plain copy 2 >>> Publisher.objects.order_by ("-name"

Add a minus sign equivalent to order BY name Desc


To limit the return data:

1 Python code 2 >>> Publisher.objects.order_by ('name') [0]   3 <Publisher:Apress>  
1 [python] view plain copy 2 >>> Publisher.objects.order_by ('name') [0]  3 <Publisher:Apress>

Equivalent to limit 1

1 Python code 2 >>> Publisher.objects.order_by ('name') [0:2]  
1 [python] view plain copy 2 >>> Publisher.objects.order_by ('name') [0:2]  

Equivalent to OFFSET 0 LIMIT 2

4. Update

1 Python code 2 >>> Publisher.objects.filter (id=52). Update (name='Apress publishing  ')  
1 [python] view plain copy 2 >>> Publisher.objects.filter (id=52). Update (name='Apress publishing' )  

 1  python code  2  >>> p = Publisher.objects.get (Name= "  apress   ")  #   first query  3  >>> p.name =  " apress Publishing   " #   update  4  >>> p.save () #   save  
1 [python] view plain copy 2 >>> p = Publisher.objects.get (name='Apress'# First Query   3'Apress publishing'# update   4 >>> p.save ()  # Save  

5. Delete

1 Python code 2 >>> p = Publisher.objects.get (name="O ' Reilly")  3 >>> P.delete ()  
1 [python] view plain copy 2 >>> p = Publisher.objects.get (name="O ' Reilly")   3 >>> p.delete ()  
1 Python code 2 >>> Publisher.objects.filter (country='USA'
1 [python] view plain copy 2 >>> Publisher.objects.filter (country='USA'). Delete ()  

Django Database basic Operations-Delete and change (tip)-Basic

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.