码迷,mamicode.com
首页 > 其他好文 > 详细

Django - ORM 练习题

时间:2018-12-07 21:09:38      阅读:275      评论:0      收藏:0      [点我收藏+]

标签:get   imp   insert   习题   查找   创建   setup   into   --   

1.modles中表结构

#出版社
class Publisher(models.Model):
    name = models.CharField(max_length=32)
    city = models.CharField(max_length=32)

    def __str__(self):
        return  "<Publisher object: {} {}>".format(self.id, self.name)
#书籍
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)
    #创建外键,关联Publisher
    Publisher = models.ForeignKey(to=Publisher)

    def __str__(self):
        return "<Book object: {} {}>".format(self.id, self.title

#作者
class Author(models.Model):
    name = models.CharField(max_length =32)
    age = models.IntegerField()
    phone = models.CharField(max_length=11)
    #创建多对多关联
    books = models.ManyToManyField(to=Book)

    def __str__(self):
        return "<Author object: {} {}>".format(self.id, self.name)

2.题目

技术分享图片
"""
查找所有书名里包含金老板的书
查找出版日期是2018年的书
查找出版日期是2017年的书名
查找价格大于10元的书
查找价格大于10元的书名和价格
查找memo字段是空的书

查找在北京的出版社
查找名字以沙河开头的出版社

查找“沙河出版社”出版的所有书籍
查找每个出版社出版价格最高的书籍价格
查找每个出版社的名字以及出的书籍数量

查找作者名字里面带“小”字的作者
查找年龄大于30岁的作者
查找手机号是155开头的作者
查找手机号是155开头的作者的姓名和年龄

查找每个作者写的价格最高的书籍价格
查找每个作者的姓名以及出的书籍数量

查找书名是“跟金老板学开车”的书的出版社
查找书名是“跟金老板学开车”的书的出版社所在的城市
查找书名是“跟金老板学开车”的书的出版社的名称
查找书名是“跟金老板学开车”的书的出版社出版的其他书籍的名字和价格

查找书名是“跟金老板学开车”的书的所有作者
查找书名是“跟金老板学开车”的书的作者的年龄
查找书名是“跟金老板学开车”的书的作者的手机号码
查找书名是“跟金老板学开车”的书的作者们的姓名以及出版的所有书籍名称和价钱

"""
题目

3.测试数据

技术分享图片
-- ----------------------------
-- Records of app01_author
-- ----------------------------
INSERT INTO `app01_author` VALUES (1, 金老板, 18, 15512351234);
INSERT INTO `app01_author` VALUES (2, 小哪吒, 20, 15312341234);
INSERT INTO `app01_author` VALUES (3, Alex, 73, 15512341234);
 
-- ----------------------------
-- Records of app01_publisher
-- ----------------------------
INSERT INTO `app01_publisher` VALUES (1, 沙河出版社, 北京);
INSERT INTO `app01_publisher` VALUES (2, 西二旗出版社, 北京);
INSERT INTO `app01_publisher` VALUES (3, 张江出版社, 上海);
INSERT INTO `app01_publisher` VALUES (4, 沙河出版社, 上海);
 
-- ----------------------------
-- Records of app01_book
-- ----------------------------
INSERT INTO `app01_book` VALUES (1, 跟金老板学开车, 2018-08-03, 12.90, null, 1);
INSERT INTO `app01_book` VALUES (2, 跟金老板学开潜艇, 2017-08-10, 9.99, null, 1);
INSERT INTO `app01_book` VALUES (3, 跟老男孩学思想, 2018-09-03, 39.99, null, 2);
INSERT INTO `app01_book` VALUES (4, 跟egon学喊麦, 2018-06-12, 0.99, null, 4);
 
-- ----------------------------
-- Records of app01_book_author
-- ----------------------------
INSERT INTO `app01_book_author` VALUES (3, 1, 1);
INSERT INTO `app01_book_author` VALUES (4, 2, 1);
INSERT INTO `app01_book_author` VALUES (5, 1, 2);
INSERT INTO `app01_book_author` VALUES (2, 2, 2);
INSERT INTO `app01_book_author` VALUES (6, 3, 3);
INSERT INTO `app01_book_author` VALUES (7, 3, 4);
测试数据

4.答案

技术分享图片
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "ORMHomework.settings")
import django
django.setup()

from app01 import models
from django.db.models import Max, Min, Sum, Avg, Count

# 查找所有书名里包含金老板的书
ret1= models.Book.objects.filter(author__name__contains=金老板)
# print (ret1)

#查找出版日期是2018年的书
ret2 = models.Book.objects.filter(publish_date__contains=2018)
# print (ret2)

#查找出版日期是2017年的书名
ret3 = models.Book.objects.filter(publish_date__contains=2017)
# print (ret3)

#查找价格大于10元的书
ret4 =models.Book.objects.filter(price__gt=10)
# print(ret4)

#查找价格大于10元的书名和价格
ret5 = models.Book.objects.filter(price__gt=10).values(title,price)
# print(ret5)

# 查找memo字段是空的书
ret6 = models.Book.objects.filter(memo=None)
print(ret6)

#-----------------------------------------------------------------------------
# 查找在北京的出版社
ret7 =models.Publisher.objects.filter(city=北京)
# print(ret7)

# 查找名字以沙河开头的出版社
ret8 = models.Publisher.objects.filter(name__startswith=沙河)
# print(ret8)

#查找“沙河出版社”出版的所有书籍
ret9 =models.Book.objects.filter(Publisher__name=沙河出版社)
# print(ret9)

# 查找每个出版社出版价格最高的书籍价格
# ret10 = models.Publisher.objects.all().annotate(max=Max(‘book__price‘)).values(‘name‘,‘max‘)
# for i in ret10 :
#     print(i)

# 查找每个出版社的名字以及出的书籍数量
# ret11 = models.Publisher.objects.all().annotate(count=Count(‘book__title‘)).values(‘name‘,‘count‘)
# for i  in ret11:
#     print(i)

#---------------------------------------------------------------------------------------------------------
#查找作者名字里面带“小”字的作者
ret12 = models.Author.objects.filter(name__contains=)
# print(ret12)

#查找年龄大于30岁的作者
ret13 = models.Author.objects.filter(age__gt=30)
# print(ret13)

#查找手机号是155开头的作者
ret14 = models.Author.objects.filter(phone__startswith=155)
# print(ret14)

#查找手机号是155开头的作者的姓名和年龄
ret15 = models.Author.objects.filter(phone__startswith=155).values(name,age)
# print(ret15)

#查找每个作者写的价格最高的书籍价格
# ret16 = models.Author.objects.all().annotate(max=Max(‘books__price‘)).values(‘name‘,‘max‘)
# for i in ret16:
#     print(i)

#查找每个作者的姓名以及出的书籍数量
# ret17 = models.Author.objects.all().annotate(count=Count(‘books__title‘)).values(‘name‘,‘count‘)
# for i in ret17 :
#     print(i)

#-------------------------------------------------------------------------------------------------------
#查找书名是“跟金老板学开车”的书的出版社
ret18 = models.Publisher.objects.filter(book__title=跟金老板学开车)
# print (ret18)

#查找书名是“跟金老板学开车”的书的出版社所在的城市
ret19 = models.Publisher.objects.filter(book__title=跟金老板学开车).values(city)
# print(ret19)

#查找书名是“跟金老板学开车”的书的出版社的名称
ret20 = models.Publisher.objects.filter(book__title=跟金老板学开车).values(name)
# print(ret20)

#查找书名是“跟金老板学开车”的书的出版社出版的其他书籍的名字和价格
# pub_obj = models.Publisher.objects.get(book__title=‘跟金老板学开车‘)
# ret21= pub_obj.book_set.all().values(‘title‘,‘price‘)
# print(ret21)

#查找书名是“跟金老板学开车”的书的所有作者
# ret22 = models.Author.objects.filter(books__title=‘跟金老板学开车‘)
# print(ret22)

#查找书名是“跟金老板学开车”的书的作者的年龄
# ret23 = models.Author.objects.filter(books__title=‘跟金老板学开车‘).values(‘name‘,‘age‘)
# print(ret23)

#查找书名是“跟金老板学开车”的书的作者的手机号码
# ret24 = models.Author.objects.filter(books__title=‘跟金老板学开车‘).values(‘name‘,‘phone‘)
# print(ret24)

#查找书名是“跟金老板学开车”的书的作者们的姓名以及出版的所有书籍名称和价钱

# ret25= models.Author.objects.filter(books__title=‘跟金老板学开车‘)
# print(ret25)
# for i in ret25:
#     print(i.books.all().values(‘title‘,‘price‘))
答案

 

Django - ORM 练习题

标签:get   imp   insert   习题   查找   创建   setup   into   --   

原文地址:https://www.cnblogs.com/wcx666/p/10084277.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!