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