ORM check 2

    # # Objects look forward lookup 
    # book_obj = models.Book.objects.get (the above mentioned id = 3) 
    # Print (book_obj) 
    # RET = book_obj.publisher.name 
    # Print (RET) 
    # # look forward lookup field 
    # RET = models.Book.objects.filter (= ID. 3) .values_list ( "publisher__name") 
    # Print (RET) 

    # # reverse lookup 
    # publisher_obj = models.Publisher.objects.get (= ID. 1) 
    # RET = publisher_obj.books .all (). values_list ( "title") 
    # Print (RET) 
    # # field to find 
    # RET = models.Publisher.objects.filter (the above mentioned id = 1) .values_list ( "books__title") 
    # Print (RET) 

    # pairs many
    # # Query 
    author_obj = models.Author.objects.get (the above mentioned id = 2 )
     # Print (author_obj.name) 
    # RET = author_obj.book.all () 
    # Print (RET) 

    # the Create create a new object 1, created in the book a book, adding the association table associated with the operation of the book and author 
    # author_obj.book.create (title = "child", publisher_id =. 3) 

    # the Add must be added to the object or ID 
    # book_obj models.Book.objects.get = ( = 12 is ID) 
    # author_obj.book.add (book_obj) 
    # # plurality of the Add 
    # book_obj = models.Book.objects.filter (id__gt =. 5, id__lt =. 8) 
    # Print (book_obj) 
    # author_obj.book.add (* book_obj) 
    # author_obj.book.add (12)

    # # set 必须要是一个可迭代对象
    # author_obj.book.set([8, 11])
    # author_obj.save()

    # remove 对象或id
    # # 对象
    # book_obj = models.Book.objects.get(id=7)
    # author_obj.book.remove(book_obj)
    # id
    # author_obj.book.remove(5)

    # # clear
    # new_author_obj = models.Author.objects.get(id=5)
    # new_author_obj.book.clear()

    # 聚合
    from django.db.models import Avg, Sum, Max, Min, Count
    # ret = models.Book.objects.all().aggregate(price_avg = Avg("price"))
    #Print (RET) 
    # RET = models.Book.objects.all (). Aggregate (MIN_PRICE = Min ( ". price")) 
    # Print (RET) 
    # RET = models.Book.objects.all (). Aggregate (= PRICE_MAX max ( ". price"), PRICE_MIN = Min ( ". price"), price_avg = Avg ( ". price"), price_sum = Sum ( ". price")) 
    # Print (RET) 

    # grouping queries 
    # query author of each book number 
    # RET = models.Book.objects.all () Annotate (author_num the Count = ( "author")). 
    # for Book in RET: 
    #      Print ( "title: {}, number: {}." the format ( book.title, book.author_num)) 
    # query is greater than the number of books 1 
    # RET = models.Book.objects.all (). Annotate (author_num = the Count ( "author")).filter (author_num__gt = 1) 
    # quality (right)
    # Query each of the book's total price 
    # RET = models.Author.objects.all (). Annotate (price_sum = Sum ( "book__price")). Values_list ( "name", "price_sum") 
    # Print (RET) 

    # F and Q 
    # check out all the books sell inventory number is greater than the number (two fields for comparison) 
    from django.db.models Import F
     # RET = models.Book.objects.filter (remain__gt = F ( "would like to purchase") ) 
    # Print (RET) 
    models.Book.objects.update (would like to purchase = (F ( " would like to purchase " ) +1) * 3)

 

Guess you like

Origin www.cnblogs.com/wt7018/p/11267648.html