Django基础6
1.模板的继承(使用频率较高) 2.模板的导入 3.数据库正向与反向迁移 4.单表查询前期准备 5.django脚本环境 6.单表查询关键字 7.单表查询之神奇的双下线查询 8.多表查询之外键字段的增删改查 1.模板的继承(使用频率较高) 需求: 页面主体不变 部分区域变化 模板继承1.需要现在母版中使用block提前划定区域 {% block 区域名称 %} {% endblock %} 2.在子版中先继承再局部替换 {% extends '母版.html' %} {% block 区域名称 %} {% endblock %} 总结母版一般情况下都应该至少含有三个区域 1.css区域 2.content区域 3.js区域 补充{{ block.super }} 调用母版内容 2.模板的导入(了解) {% include 'myform.html' %} 3.数据库正向与反向迁移 正向迁移(将django中的类导入到MySQL中变成表) python3 manage.py makemigrations python3 manage.py migrate 反向迁移inspectdb 4.单表查询预备知识 from django.db import models # Create your models here. class Books(models.Model): title = models.CharField(max_length=32) price = models.DecimalField(max_digits=8,decimal_places=2) # 日期类型 publish_time = models.DateField(auto_now_add=True) """ auto_now:每次修改数据之后都会自动更新当前时间 auto_now_add:第一次创建数据的时候自动记录时间 """ # publish_time = models.DateTimeField() def __str__(self): return self.title # 只能返回字符串类型的数据 class Book(models.Model): title = models.CharField(max_length=32) price = models.DecimalField(max_digits=8, decimal_places=2) # 日期类型 publish_time = models.DateField(auto_now_add=True) publish = models.ForeignKey(to='Publish') authors = models.ManyToManyField(to='Author') def __str__(self): return self.title # 只能返回字符串类型的数据 class Publish(models.Model): title = models.CharField(max_length=32) addr = models.CharField(max_length=64) def __str__(self): return self.title class Author(models.Model): name = models.CharField(max_length=32) age = models.IntegerField() author_detail = models.OneToOneField(to='AuthorDetail') def __str__(self): return self.name class AuthorDetail(models.Model): phone = models.BigIntegerField() addr = models.CharField(max_length=64) def __str__(self): return self.addr 5.django脚本环境 django默认情况下是不允许单独使用某个功能部分(models.py) 解决方法: 1.要么自己新建一个py文件 2.要么使用自带的tests文件 import os if __name__ == "__main__": os.environ.setdefault("DJANGO_SETTINGS_MODULE", "day47.settings") import django django.setup() 6.单表查询关键字 # 增 1.create() # models.Books.objects.create(title='三国演义',price=456.23) # models.Books.objects.create(title='水浒传',price=876.45) # models.Books.objects.create(title='聊斋志异',price=123.69) # models.Books.objects.create(title='草堂笔记',price=456.96) # 查 2.all() # res = models.Books.objects.all() # print(res) # QuerySet对象 # print(res.query) # 只要是QuerySet对象就可以点query查看内部SQL语句 # 查 3.filter() # res1 = models.Books.objects.filter() # pk特指当前表的主键字段 # print(res1) # QuerySet对象 # print(res1.query) # print(res1.first()) # 获取列表中第一个数据对象 # print(res1.last()) # 获取列表中最后一个数据对象 # 4.values与5.values_list # res2 = models.Books.objects.values('title','price') # print(res2) # QuerySet对象 可以看成列表套字典 # print(res2.query) # res3 = models.Books.objects.values_list('title','price') # print(res3) # QuerySet对象 可以看成列表套元祖 # print(res3.query) # 6.查 get() 不推荐使用 # res4 = models.Books.objects.get(pk=1) # print(res4) # 数据对象 # res5 = models.Books.objects.get(pk=100) # print(res5) # 数据对象 # res6 = models.Books.objects.filter(pk=100) # print(res6) # 数据对象 # 7.取反 exclude() # res7 = models.Books.objects.exclude(pk=1) # print(res7) # QuerySet # print(res7.query) # 8.排序 order_by() 默认是升序(asc) 字段前面加负号降序(desc) # res8 = models.Books.objects.order_by('price') # res8 = models.Books.objects.order_by('-price') # select * from books order by price desc,price asc; # res8 = models.Books.objects.order_by('-price','price') # print(res8) # QuerySet对象 # print(res8.query) # 9.反转 reverse() 必须先有顺序才可以反转 # res9 = models.Books.objects.all() # res9 = models.Books.objects.order_by('price').reverse() # print(res9) # 10.去重 distinct() # res10 = models.Books.objects.all().distinct() # res10 = models.Books.objects.values('title','price').distinct() # print(res10) # 11.计数 count() # res11 = models.Books.objects.count() # print(res11) # 6 # 12.判断是否有数据 exists() # res12 = models.Books.objects.filter(pk=999).exists() # print(res12) # False # 13.update() # 14.delete() 7.单表查询之神奇的双下划线 # 查询筛选价格的书籍 # res = models.Books.objects.filter(price__gt=200) # print(res) # res1 = models.Books.objects.filter(price__lt=200) # print(res1) # res2 = models.Books.objects.filter(price__gte=456.23) # print(res2) # res3 = models.Books.objects.filter(price__lte=456.23) # print(res3) # 成员运算 # res4 = models.Books.objects.filter(price__in=(456.23,111)) # print(res4) # 范围查询 # res5 = models.Books.objects.filter(price__range=(100,456.23)) # print(res5) # 模糊查询 # 查询书籍名称中含有字母a的书 # res6 = models.Books.objects.filter(title__contains='a') # print(res6) # 区分 # res7 = models.Books.objects.filter(title__icontains='a') # print(res7) # 忽略大小写 # 日期相关 # 查看出版月份是五月的书 # res8 = models.Books.objects.filter(publish_time__month=5) # print(res8) # print(res8.query) # res9 = models.Books.objects.filter(publish_time__year=2021) # print(res9) 8.多表查询之外键字段增删改查 # 增 # models.Book.objects.create(title='三国演义',price=345.43,publish_id=1) # models.Book.objects.create(title='红楼梦',price=678.31,publish_id=2) # publish_obj = models.Publish.objects.filter(pk=2).first() # models.Book.objects.create(title='三国演义',price=345.43,publish=publish_obj) # models.Book.objects.create(title='七龙珠',price=908.43,publish=publish_obj) # 改 # models.Book.objects.filter(pk=2).update(publish_id=1) # models.Book.objects.filter(pk=2).update(publish=publish_obj) # 删 级联更新级联删除 # models.Publish.objects.filter(pk=1).delete() # 多对多 book_obj = models.Book.objects.filter(pk=3).first() # 绑定关系 # book_obj.authors.add(1) # 去书与作者的关系表中绑定关系 # book_obj.authors.add(1,2) # 去书与作者的关系表中绑定关系 # book_obj.authors.add(author_obj1) # book_obj.authors.add(author_obj1,author_obj2) # 修改关系 # book_obj.authors.set([1,]) # book_obj.authors.set([1,2]) # book_obj.authors.set([author_obj,]) # book_obj.authors.set([author_obj1,author_obj2]) # 移除关系 # book_obj.authors.remove(1) # book_obj.authors.remove(1,2) # book_obj.authors.remove(author_obj,) # book_obj.authors.remove(author_obj1,author_obj2) # 清空关系 book_obj.authors.clear()