Django ORM练习题
表结构 # 书 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) # 创建外键,关联publish publisher = models.ForeignKey(to="Publisher") # 创建多对多关联author author = models.ManyToManyField(to="Author") def __str__(self): return "<Book object: {} {}>".format(self.id, self.title) # 出版社 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 Author(models.Model): name = models.CharField(max_length=32) age = models.IntegerField() phone = models.CharField(max_length=11) def __str__(self): return "<Author object: {} {}>".format(self.id, self.name) 数据:
-- ---------------------------- -- 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', '1', '2'); INSERT INTO `app01_book_author` VALUES ('5', '2', '1'); 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', '4', '3');
练习题 """ 查找所有书名里包含金老板的书 ret = models.Book.objects.filter(title__contains='金老板') print(ret) 查找出版日期是2018年的书 ret = models.Book.objects.filter(pulishe_date__year=2018) 查找出版日期是2017年的书名 ret = models.Book.objects.filter(publisher_date__year=2017).values('title') print(ret) 查找价格大于10元的书 ret = models.Book.objects.filter(price__gt=10) print(ret) 查找价格大于10元的书名和价格 ret = models.Book.objects.filter(price__gt=10).values('title','price') print(ret) 查找memo字段是空的书 ret = models.Book.objects.filter(Q(memo='')|Q(memo__isnull=True)) print(ret) 查找在北京的出版社 ret = models.Publisher.objects.filter(city='北京') print(ret) 查找名字以沙河开头的出版社 ret = models.Publisher.objects.filter(name__startwith='沙河').values('name').distinct() 查找“沙河出版社”出版的所有书籍 ret = models.Book.objects.filter(publisher__name=“沙河出版社”) ret = models.Publisher.objects.filter(name='沙河出版社').values(''book__title') 查找每个出版社出版价格最高的书籍价格 ret = models.Publisher.objects.annotate(max=Max('book__price')).values('name','max') print(ret) 查找每个出版社的书名以及出的书籍数量 ret =models.Publisher.objects.annotate(count=Count('book')).values('count','name') 查找作者名字里面带“小”字的作者 ret = models.Author.objects.filter(name__contains='小') print(ret) 查找年龄大于30岁的作者 ret = models.Author.objects.filter(age__gt=30) print(ret) 查找手机号是155开头的作者 ret = models.Author.objects.filter(phone__startwith='155') print(ret) 查找手机号是155开头的作者的姓名和年龄 ret = models.Author.objects.filter(phone__startwith=155).values('name','age') print(ret) 查找每个作者写的价格最高的书籍价格 ret = models.Author.objects.annotate(max_price=Max('book__price')).values('name','max_price' ) print(ret) 查找每个作者的姓名以及出的书籍数量 ret=models.Author.objects.annotate(count=Count('book')).values('name','count') print(ret) 查找书名是“跟金老板学开车”的书的出版社 ret = models.Book.objects.get(title='跟金老板学开车').publisher ret = models.Publisher.objects.get(book__title='跟金老板学开车') print(ret) 查找书名是“跟金老板学开车”的书的出版社所在的城市 ret = models.Book.object.get(name='跟金老板学开车')==== 查找书名是“跟金老板学开车”的书的出版社的名称 ret= models.Publisher.objects.filter(book__title=“跟金老板学开车”) 查找书名是“跟金老板学开车”的书的出版社出版的其他书籍的名字和价格 ret = models.Book.objects.filter(publisher=models.Publisher.objects.get(book__title='跟金老板学开车')).exclude( title='跟金老板学开车').values('title', 'price') print(ret) ret = models.Publisher.objects.get(book__title='跟金老板学开车').book_set.exclude(title='跟金老板学开车').values('title', 'price') print(ret) 查找书名是“跟金老板学开车”的书的所有作者 ret = models.Author.objects.all(book__title='跟金老板学开车') 查找书名是“跟金老板学开车”的书的作者的年龄 ret = models.Author.objects.filter(book__title=“跟金老板学开车”).values('age' 查找书名是“跟金老板学开车”的书的作者的手机号码 ret = models.Author.objects.filter(book__title=“跟金老板学开车”).values('phone') 查找书名是“跟金老板学开车”的书的作者们的姓名以及出版的所有书籍名称和价钱 ret = models.Author.objects.get(book__title=“跟金老板学开车”).values('name', 'book__title', 'book__price').distinct() ret = models.Book.objects.get(title='跟金老板学开车').author.values('name','book__title','book__price').distinct """
posted on 2018-09-17 21:27 liangliang123456 阅读(235) 评论(0) 收藏 举报
浙公网安备 33010602011771号