首页 编程教程正文

Django ORM 练习题及答案

piaodoo 编程教程 2020-02-02 11:56:35 1023 0 python教程

这篇文章主要介绍了Django ORM 练习题及答案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

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(title__contains='金老板')
# print (ret1)

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

#查找出版日期是2017年的书名
ret3 = models.Book.objects.filter(publish_date__year=2017).values('title')
# 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__isnull=True)
# 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')
# ret10 = models.Publisher.objects.annotate(max=Max('book_price')).values()
# for i in ret10 :
#   print(i)

# 查找每个出版社的名字以及出的书籍数量
ret11 = models.Publisher.objects.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.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)

#查找每个作者的姓名以及出的书籍数量
# 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().exclude(title='跟金老板学开车').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'))

#
# ret = models.Book.objects.aggregate(Max('price'))
# print(ret)

ret25 = models.Author.objects.values('name','books__title','books__price').filter(books__title='跟金老板学开车')
print(ret25)

答案

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

版权声明:

本站所有资源均为站长或网友整理自互联网或站长购买自互联网,站长无法分辨资源版权出自何处,所以不承担任何版权以及其他问题带来的法律责任,如有侵权或者其他问题请联系站长删除!站长QQ754403226 谢谢。

有关影视版权:本站只供百度云网盘资源,版权均属于影片公司所有,请在下载后24小时删除,切勿用于商业用途。本站所有资源信息均从互联网搜索而来,本站不对显示的内容承担责任,如您认为本站页面信息侵犯了您的权益,请附上版权证明邮件告知【754403226@qq.com】,在收到邮件后72小时内删除。本文链接:http://www.piaodoo.com/447.html

评论

搜索