Django model layer (the models.py) Operation table as much

Adding table records

Many:

    # Mode. 1: 
    publish_obj = Publish.objects.get (= ID. 1) 
    Book.objects.create (name = '2 Books', =. Price 200 is, publishs = publish_obj) 
    # mode 2: 
    Book.objects.create (name = '3 Books', price = 300, publish_id = 1)

 Many to many:

    book_obj = Book.objects.filter(name='2号图书').first()
    author1_obj = Author.objects.get(name='1号作者')
    author2_obj = Author.objects.get(name='2号作者')
    book_obj.authors.add(author1_obj,author2_obj)

Lookup table records

  • Mode 1: Object-based
  • Option 2: Based on double underscore

One:

    Reverse query # 
    # mode. 1: 
    author_detail_list = AuthorDetail.objects.filter (addr = 'address') 
    for obj in author_detail_list: 
        Print (obj.author.name) 
    # mode 2: 
    AuthorDetail.objects.filter (author__name =' 2 author ') .values ( "addr") 
    # forward queries 
    # mode 1: 
    Author.objects.filter (name ='. author No. 1 ') .first () authordetail.addr 
    # mode 2: 
    Author.objects.filter ( name = '1 author') .values ( "authordetail__addr")

 Many:

# Forward queries (by field) 
# 1 way: 
# Query No. 1 book publisher name 
Book.objects.filter (the above mentioned id = 1) .first () publishs.name. 
# 2 ways: 
# Query No. 1 book publishing agency name and address 
Book.objects.filter (the above mentioned id = 1) .values_list ( "publishs__name", "publishs__addr") 

# reverse lookup (by table name: table name _set) 
# 1 way: 
# 1 query Publishing All books 
Publish.objects.get (name = 'Press 1') .book_set.all () 
# 2 ways: 
# query No. 1 book publisher's name and address 
Publish.objects.filter (book__id = 1) .values_list ( "name", "addr ")

Many to many:

   With many

 Aggregate query:

  aggregate(*args, **kwargs)

    Returns a dictionary, a polymerization key identifier value, can be specified freely, may not specify

  example:

# Do not specify a key 
Book.objects.all (). Aggregate (Avg ( ". Price")) 
# specified key 
Book.objects.all (). Aggregate (price_avg = Avg ( "price"))

 Grouping queries:

  example:

    Single table # 
    # Book.objects.values RET = ( ". Price"). Annotate (the Count ( "ID")) 
    # polyepitopic 
    ret = AuthorDetail.objects.values ( "addr") . Annotate (c = Count ( "author ")). values (" addr "," C ") 
    # polyepitopic identical to join association table packet, then the query packet

 F query:

  Comparison is made between the field values, between the support and the objects constant, between the object and the object and the addition, subtraction modulo operation.

  example:

F django.db.models Import from 
# inquiry number is greater than the number of browsers read the 
Book.objects.filter (readNum__lt = F ( 'scanNum '))

 Q query:

  Support and, or, not, and other complex queries, parameters can be used with keywords, but the object to be placed in front of Q

  example:

from django.db.models import Q
Book.objects.filter(Q(authors__name="2号作者")|Q(authors__name="1号作者"))

 

 

Guess you like

Origin www.cnblogs.com/shannen/p/11285154.html