Python-day60 Object Relational Mapping (ORM) ORM even table query ORM Advanced

Source: Internet
Author: User

ImportOSif __name__=='__main__':    #Specifies the Django project configuration information that the current py script needs to loadOs.environ.setdefault ('Django_settings_module','bookmanagesystem.settings')    #start a Django project    ImportDjango Django.setup () fromApp01ImportModels#Many-to-many operations    #1.create    #have done two things    #1. Created a new author named Ker1 2. Associate the newly created author with the first book    #ret = models. Book.objects.first (). Author.create (    #name= ' Ker1 ',    #age=18,    #phone= ' 1516165 ',    #detail_id=3    # )    #Find all the authors of the first book, and ID    #ret = models. Book.objects.first (). Author.all ()    #ret = models. Book.objects.first (). Author.all (). VALUES (' ID ')    #print (ret)    #2.set    #by setting the author of the first book with IDs 3 and 8    #models. Book.objects.first (). Author.set ([3,8])    #query all the IDs of the first book    #ret = models. Book.objects.first (). Author.all (). VALUES (' ID ')    #print (ret)    #3. Add    #Add an Author object with ID 8 to the first book    #models. Book.objects.first (). Author.add (8)    #ret = models. Book.objects.first (). Author.all (). VALUES (' ID ')    #print (ret)    #4.remove    #Remove the author of the first book with a foreign key ID of 3    #models. Book.objects.first (). Author.remove (3)    #ret = models. Book.objects.first (). Author.all (). VALUES (' ID ')    #print (ret)    #5.clear    #Clear all authors of the first book    #models. Book.objects.first (). Author.clear ()    #ret = models. Book.objects.first (). Author.all (). VALUES (' ID ')    #print (ret)    #6.all    #View all the contents of the first book's Objects    #ret = models. Book.objects.first (). Author.all ()    #print (ret)    #Aggregation    #Check the total price of all books     fromDjango.db.modelsImportAVG, Sum, Max, Min, Count#need to call aggregate (alias = inside write aggregate function (' condition ') before using aggregate function)    #Find out the total price of all the books    #ret = models. Book.objects.aggregate (sum_price=sum (' price '))    #print (ret)    #view average price \ maximum price \ Minimum price    #ret = models. Book.objects.aggregate (Avg_pirce=avg (' price '),    #Max_price=max (' price '),    #min_price=min (' price '))    #print (ret)    #    ## Number of authors per book    #ret = models. Book.objects.annotate (C=count (' author ')). VALUES (' title ', ' C ' )    #print (ret)    #    ## Count the prices of the cheapest books that each publisher buys    #ret = models. Publisher.objects.annotate (min_price=min (' Books__price ')). VALUES (' name ', ' Min_price ')    #print (ret)    #count more than one author's book (The number of authors is greater than 1)    #ret = models. Book.objects.annotate (C=count (' author ')). Filter (C__gt=1)    #print (ret)    #Sort by the number of book authors    #ret = models. Book.objects.annotate (C=count (' author ')). order_by (' C ' )    #print (ret)    #Query the total price of each author's books Book__price All prices in the Book table    #ret = models. Author.objects.annotate (sum_price=sum (' Book__price ')). VALUES (' name ', ' Sum_price ')    #print (ret)

Python-day60 Object Relational Mapping (ORM) ORM even table query ORM Advanced

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.