Main content: https://www.cnblogs.com/maple-shaw/articles/9403501.html
Note : If you want to run the entire Django project in a file:
You need to write to the file:
Import osif __name__ = = "__main__": os.environ.setdefault ("Django_settings_module", "orm_operate.settings") Import Django django.setup ()
1 orm commonly used 13 query statements: Divided into the following four kinds of
A: Get the list of objects:
obj = models. Person.objects. All() Obj1 = models. Person.objects.Filter(id=1) Obj2 = models. Person.objects.Exclude(Name= ' Alex ') # values are special, the object list is no longer an object, but a dictionary, with the built-in and value OBJ3 = models. Person.objects.all ().values ()# Value_list is also very special, the object list is not an object, but a meta-ancestor, which contains the value of each row, Obj4 = models. Person.objects.all ().values_list ()# print (OBJ4) # Sorts the objects, the-number indicates that the reverse order can be taken. Obj5 = models. Person.objects.all ().order_by(' id ') # print (OBJ5) # must be sorted on the basis of an existing sort # obj6 = models. Person.objects.all (). order_by (' id '). Reverse ()# The second method: You can also set the ordering in the Mata method in models according to what sort Obj6 = models. Person.objects.all (). Reverse () # print (OBJ6)
B: Get the object:
# If the condition does not exist or produces more than one data will be an error, generally not recommended to use. OBJ7 = models. Person.objectsget (id=1) # First gets the last object, which is the corresponding elast. Obj8 = models. Person.objects.all (). first () obj9 = models. Person.objects.all (). last () # obj10 = models. PERSON.OBJECTS.Create(name= ', age=25) # obj10.save () # Print (obj10) # distinct to heavy
C: Return number
OBJ11 = models. Person.objects.all (). Count ()
D: Returns a Boolean value
OBJ12 = models. Person.objects.exclude (id=1). Exists ()
2 FOREIGN Key Query
Class book (Models. Model): id = models. Autofield (primary_key=true) title = models. Charfield (max_length=32) Press = models. ForeignKey (to= ' Press ', related_name= ' books 'null=true)
Forward query:
1. The difference between press_id and press.id
# obj1 = models. Book.objects.get (id=1) # Print (obj1.press.id) #从book表中取到press_id, and then find the ID in the press table = press_id Object out press.id # Print (obj1.press_id) #直接从book表里面取数据, find once
2. Association fields : associated fields __ Fields (cross-table)
Find all the books published by Hunan publishing house books = models. Book.objects.filter (press_id= (models. Press.objects.get (name= ' Hunan satellite TV Press '). ID)
Print (books) execution process: First remove the ID from the press table, according to ID = press_id and then from the Book table title Books = Models. Book.objects.filter (press__name= ' Hunan Satellite TV Press ') #使用了连表查询, check once. Print (books)
Print (models. Book.objects.values_list ("Press__name"))
Reverse Query :
1. Get Management Objects :
Press_obj = press.objects.get (id = 1)
Press_obj.books Gets a management object that sets Related_name= ' books '
Press_obj.books.all () List of book objects associated with the publishing house
2. To set and delete a corresponding relationship:
Obj1 = press.objects.get (id = 2)
Obj1.books.set (Book.objects.filter (id__in = [4, 5]))
Obj1.books.remove (*book.objects.filter (id__in = [4, 5])) Press field set, null = True
3. Associated fields:
Find the publisher of the book of Happy base
obj = models. Press.objects.filter (id=models. Book.objects.get (title= ' Happy Camp '). press_id)
Print (obj)
obj = models. Press.objects.filter (books__title= ' Happy Camp '), #涉及到了一个连表查询, check once
Print (obj)
More than 3 pairs of queries:
obj = models. Author.objects.get (id__in=[1]) # print (obj) # Print (obj.name) print (Obj.books.all ()) # Obj.books.set ([2, 3]) # sets the corresponding relationship # obj.books.add (2,3) obj.books.remove (*models. Book.objects.all ()) #删除对应关系
ORM13 query statement, foreign key query, multi-query single-table query double underline method