Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of ORM exercises in Django

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces the example analysis of ORM exercises in Django, which has a certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article.

Table structure in 1.modles

# Publishing House class Publisher (models.Model): name = models.CharField (max_length=32) city = models.CharField (max_length=32) def _ str__ (self): return "" .format (self.id, self.name) # Book class Book (models.Model): title = models.CharField (max_length=32) publish_date = models.DateField (auto_now_add=True) price = models.DecimalField (max_digits=5,decimal_places=2) memo = models.TextField (null=True) # create a foreign key Association Publisher Publisher = models.ForeignKey (to='Publisher') def _ _ str__ (self): return "" .format (self.id, self.title# author class Author (models.Model): name = models.CharField (max_length= 32) age = models.IntegerField () phone = models.CharField (max_length=11) # create a many-to-many association books = models.ManyToManyField (to='Book') def _ _ str__ (self): return ".format (self.id, self.name)

two。 Title

Search for titles of all books including Boss Jin's book, search for books published in 2018, search for books with a publication date of 2017, search for books with a price greater than 10 yuan, find titles and prices for books with a price greater than 10 yuan, search for books with empty memo fields, search for publishers in Beijing, search for all books published by Shahe Publishing House, find all books published by Shahe Publishing House. The highest price of books look for the name of each publisher and the number of books published, look for the author with the word "small" in the name of the author, look for the author older than 30 years old, look for the author whose mobile number is 155, look for the name and age of the author whose mobile number begins with 155, find the name and age of each author, find the name of each author and the number of books out. The publishing house of the book title is "learn to drive from Boss Jin" look for the city where the title of the book is "learn to drive from Boss Jin". Look for the title of the book "learn to drive from Boss Jin". Look up the names and prices of other books published by publishers whose title is "learn to drive from Boss Jin". Look up all the authors of books entitled "learn to drive from Boss Jin". Look for the age of the author of the book entitled "learn to drive from Boss Jin". Look up the mobile phone number of the author of the book entitled "learn to drive from Boss Jin". Look up the names of the authors of the book, as well as the titles and prices of all books published.

3. test data

-Records of app01_author-- INSERT INTO `app01_ author`VALUES ('1Qing,' Boss Jin', '18Boss,' 15512351234'); INSERT INTO `app01_ author`VALUES ('2fang,' Xiao Nazhi', '20mm,' 15312341234') INSERT INTO `app01_ author `VALUES ('313,' Alex','73, '15512341234');-Records of app01_publisher-- INSERT INTO `app01_ publisher` VALUES ('1Qing,' Shahe Publishing House', 'Beijing') INSERT INTO `app01_ publisher` VALUES ('2Qing,' Xi Erqi Publishing House', 'Beijing'); INSERT INTO `app01_ Publishing VALUES ('3Qing,' Zhangjiang Publishing House', 'Shanghai'); INSERT INTO `app01_ publisher `VALUES ('4Qing,' Shahe Publishing House', 'Shanghai') -Records of app01_book-- INSERT INTO `app01_ book`VALUES ('1x, 'learn to drive from Boss Jin', '2018-08-03,' 12.90', null,'1') INSERT INTO 'app01_ Book`VALUES (' 2', 'learn to drive a submarine from Boss Jin', '2017-08-10', '9.99', null,'1'); INSERT INTO 'app01_ Book` VALUES (' 3', 'learn ideas from an old boy', '2018-09-03,' 39.99, null,'2') INSERT INTO `app01_ book` VALUES ('44th,' learn from egon to shout wheat', '2018-06-1213,' 0.994th, null,'4') -Records of app01_book_author-- INSERT INTO `app01_book_ author`VALUES ('3clients,' 1authors,'1'); INSERT INTO `app01_book_ author`VALUES ('4clients,' 2authors,'1') INSERT INTO `app01_book_ author`VALUES ('5clients,' 1authors,'2'); INSERT INTO `app01_book_ authors`VALUES ('2levels,' 2cycles,'2'); INSERT INTO `app01_book_ authors`VALUES ('6levels,' 3authors,'3'); INSERT INTO `app01_book_ authors`VALUES ('7cycles,' 3authors,'4'); test data

4. Answer

Import osos.environ.setdefault ("DJANGO_SETTINGS_MODULE", "ORMHomework.settings") import djangodjango.setup () from app01 import modelsfrom django.db.models import Max, Min, Sum, Avg Count# finds all titles that include Boss title__contains=' ret1= models.Book.objects.filter (title__contains=' Boss Kim) # print (ret1) # find books published in 2018 ret2 = models.Book.objects.filter (publish_date__year=2018) # print (ret2) # find titles ret3 = models.Book.objects.filter (publish_date__year=2017). Values ('title') # print (ret3) # find Books with a price greater than 10 yuan ret4 = models.Book.objects.filter (price__gt=10) # print (ret4) # find titles and prices of books with a price greater than 10 yuan ret5 = models.Book.objects.filter (price__gt=10) .values ('title' 'price') # print (ret5) # find books where the memo field is empty ret6 = models.Book.objects.filter (memo__isnull=True) # print (ret6) #-# Find the publishing house in Beijing ret7 = models.Publisher.objects.filter (city=' Beijing') # print (ret7) # find the publisher whose name begins with Shahe ret8 = models.Publisher.objects.filter (name__startswith=' Shahe') # print (ret8) # find all books published by Shahe Publishing House ret9 = models.Book.objects.filter (Publisher__name=' Shahe Publishing House) # print (ret9) # find each publication The highest price of books published by the press # ret10 = models.Publisher.objects.all (). Annotate (max=Max ('book__price')) .values (' name' 'max') # ret10 = models.Publisher.objects.annotate (max=Max (' book_price')). Values () # for i in ret10: # print (I) # find the name of each publisher and the number of books published ret11 = models.Publisher.objects.annotate (count=Count ('book__title')) .values (' name') 'count') # for i in ret11:# print (I) #- -- # find the author with the word "small" in the author's name ret12 = models.Author.objects.filter (name__contains='') # print (ret12) # find the author older than 30 years old ret13 = models.Author.objects.filter (age__gt=30) # print (ret13) # find the author whose mobile number is at the beginning of 155ret14 = models.Author.objects.filter (phone__startswith=155) # print (ret14) # find the author whose mobile number is at the beginning of 155s Name and age ret15 = models.Author.objects.filter (phone__startswith=155) .values ('name' 'age') # print (ret15) # find the most expensive books written by each author # ret16= models.Author.objects.annotate (max=Max (' books__price')) .values ('name','max') ret16= models.Book.objects.values (' author') .annotate (max=Max ('price')). Values (' author') 'max') # for i in ret16:# print (I) # find the name of each author and the number of books published # ret17 = models.Author.objects.all (). Annotate (count=Count (' books__title')) .values ('name' 'count') # for i in ret17: # print (I) #- # find the publishing house ret18 = models.Publisher.objects.filter (book__title=' learn to drive from Boss Jin) # print (ret18) # find the city where the publisher of the book is titled "learn to drive from Boss Jin" ret19 = models.Publisher.objects.filter (book__title=' learn to drive from Boss Jin). Values ('city') # print (ret19) # find the title is "follow" The name of the publisher of Boss book__title=' 's book "learn to drive" is ret20 = models.Publisher.objects.filter (book__title=' learns to drive from Boss Jin). Values ('name') # print (ret20) # look up the names and prices of other books published by publishers whose title is "learn to drive from Boss Jin". Pub_obj = models.Publisher.objects.get (learn to drive from Boss Jin) ret21= pub_obj.book_set .all (). Exclude (title=' learns to drive from Boss Kim). Values ('title' 'price') print (ret21) # find all the authors of the book entitled "learn to drive from Boss Kim" # ret22 = models.Author.objects.filter (books__title=' learn to drive from Boss Jin) # print (ret22) # find the age of the author of the book entitled "learn to drive from Boss Jin" # ret23 = models.Author.objects.filter (books__title=' learn to drive from Boss Jin). Values (' name' 'age') # print (ret23) # find the mobile phone number of the author of the book entitled "learn to drive from Boss Kim": # ret24 = models.Author.objects.filter (books__title=' learns to drive from Boss Jin). Values (' name' 'phone') # print (ret24) # find the names of the authors of books titled "learn to drive from Boss Kim", as well as the titles and prices of all books published # ret25= models.Author.objects.filter (books__title=' learn to drive from Boss Jin) # print (ret25) # for i in ret25:# print (i.books.all (). Values (' title') 'price')) # # ret = models.Book.objects.aggregate (Max (' price')) # print (ret) ret25 = models.Author.objects.values ('name','books__title','books__price'). Filter (books__title=' learns to drive from Boss Jin) print (ret25) Thank you for reading this article carefully I hope the article "sample Analysis of ORM exercises in Django" shared by the editor will be helpful to you. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report