Django之ORM练习
Posted mjc69213
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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‘);
三、练习题目
查找所有书名里包含金老板的书
查找出版日期是2018年的书
查找出版日期是2017年的书名
查找价格大于10元的书
查找价格大于10元的书名和价格
查找memo字段是空的书
查找在北京的出版社
查找名字以沙河开头的出版社
查找“沙河出版社”出版的所有书籍
查找每个出版社出版价格最高的书籍价格
查找每个出版社的书名以及出的书籍数量
查找作者名字里面带“小”字的作者
查找年龄大于30岁的作者
查找手机号是155开头的作者
查找手机号是155开头的作者的姓名和年龄
查找每个作者写的价格最高的书籍价格
查找每个作者的姓名以及出的书籍数量
查找书名是“跟金老板学开车”的书的出版社
查找书名是“跟金老板学开车”的书的出版社所在的城市
查找书名是“跟金老板学开车”的书的出版社的名称
查找书名是“跟金老板学开车”的书的出版社出版的其他书籍的名字和价格
查找书名是“跟金老板学开车”的书的所有作者
查找书名是“跟金老板学开车”的书的作者的年龄
查找书名是“跟金老板学开车”的书的作者的手机号码
查找书名是“跟金老板学开车”的书的作者们的姓名以及出版的所有书籍名称和价钱
四、答案
#! /usr/bin/env python3 # -*- coding: utf-8 -*- import os if __name__ == ‘__main__‘: os.environ.setdefault("DJANGO_SETTINGS_MODULE", "day03.settings") import django django.setup() from app01 import models from django.db.models import Avg, Max, Min, Count # print(models.Book.objects.filter(author__name=‘金老板‘)) # print(models.Book.objects.filter(publish_date__year=2018)) # print(models.Book.objects.filter(publish_date__year=2017)) # print(models.Book.objects.filter(price__gt=10)) # print(models.Book.objects.filter(price__gt=10).values(‘price‘, ‘title‘)) # print(models.Book.objects.filter(memo__isnull=True)) # print(models.Publisher.objects.filter(city=‘北京‘)) # print(models.Publisher.objects.filter(name__istartswith=‘沙河‘)) # print(models.Book.objects.filter(publisher__name__istartswith=‘沙河‘)) # print(models.Book.objects.values(‘publisher__name‘).annotate(max_price = Max(‘price‘)).values(‘title‘, ‘publisher__name‘, ‘max_price‘)) # print(models.Book.objects.values(‘publisher__name‘).annotate(count=Count(‘id‘)).values(‘title‘, ‘count‘)) # print(models.Author.objects.filter(name__contains=‘小‘).values()) # print(models.Author.objects.filter(phone__istartswith=155).values_list()) # print(models.Author.objects.filter(phone__istartswith=155).values_list(‘name‘, ‘age‘)) # print(models.Book.objects.values(‘author__name‘).annotate(max=Max(‘price‘)).values(‘title‘, ‘price‘)) # print(models.Author.objects.values(‘name‘).annotate(count=Count(‘book__id‘)).values(‘name‘,‘count‘)) # print(models.Book.objects.filter(title=‘跟金老板学开车‘).values(‘publisher__name‘)) # print(models.Book.objects.filter(title=‘跟金老板学开车‘).values_list(‘publisher__city‘)) # print(models.Book.objects.filter(title=‘跟金老板学开车‘).values(‘publisher__name‘)) # print(models.Publisher.objects.filter().exclude(book__title=‘跟金老板学开车‘).values(‘book__title‘, ‘book__price‘)) # print(models.Book.objects.filter(title=‘跟金老板学开车‘).values(‘author__name‘)) # print(models.Author.objects.filter(book__title=‘跟金老板学开车‘).values(‘name‘,‘age‘)) # print(models.Publisher.objects.filter(book__title=‘跟金老板学开车‘).values(‘book__author__name‘, ‘book__author__phone‘)) # ret = models.Book.objects.filter(title=‘跟金老板学开车‘).values_list(‘author__name‘) # for i in ret: # print(models.Book.objects.filter(author__name=i[0]).values_list(‘author__name‘,‘title‘,‘price‘))
以上是关于Django之ORM练习的主要内容,如果未能解决你的问题,请参考以下文章