2018-9-17 19:53:22
预习:http://www.cnblogs.com/liwenzhou/p/8343243.html
新买个蓝牙挂耳耳机,感觉不错!
放上代码 笔记什么的明天继续 视频没看完!
最近想沉下心看书!
越努力,越幸运.永远不要高估自己!
# /usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2018/9/17 15:23 # @Author : TrueNewBee """ 这里只涉及用法,不涉及具体数据内容 因为数据库里面内容我没有添加 2018-9-17 20:23:46 """ import os from app01 import models if __name__ == '__main__': # 加载Django项目的配置信息 os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite67.settings") import django django.setup() ret = models.Employee.objects.all().values("id") print(ret) from django.db.models import Avg ret = models.Employee.objects.values("dept").annotate(avg=Avg("salary")).value("dept", "avg") print(ret) ret = models.Employee.objects.values("dept_id").annotate(avg=Avg("salary")).values("dept_name", "avg") print(ret) # 查询所有的员工和部门名称 ret = models.Employee.objects.values("name", "dept_name") print(ret) ret = models.Employee.objects.select_related().values("name", "dept_name") print(ret) ret = models.Employee.objects.select_related().values("name", "books_title") print(ret) print("="*120) ret = models.Author.objects.preftch_related().values("name", "books_title") print(ret) # 批量创建 # 有100个书籍对象 obj = [models.Book(title="沙河{}".format(i)) for i in range(100)] # 在数据库中批量创建 10次一提交 models.Book.objects.bulk_create(obj, 10)
# /usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2018/9/14 15:28 # @Author : TrueNewBee import os from app001 import models """ 2018-9-14 19:29:12 这里只涉及用法,不涉及具体数据内容 因为数据库里面内容我没有添加 """ if __name__ == '__main__': # 加载Django项目的配置信息 os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite67.settings") import django django.setup() # 一对一的查询 # 和外键查询类似 author_obj = models.Author.objects.get(id=1) obj = author_obj.detail print(obj.hobby, obj.addr) # 在app01 里面查询 id是1的作者关联的书 from app01 import models ret = models.Author.objects.get(id=1).books.all() print(ret) print("app01".center(80, "-")) # 在app02 里面查询 id是1的作者关联的书 # from app02 import models ret = models.Author2Book.objects.filter(author_id=1).values_list("book_id") # id是1的作者关联的书的id print(ret) ret = [i[0] for i in ret] # 查书这张表 ret = models.Book.objects.filter(id_in=ret) print(ret) # 在app03 里面查询id 是1的作者关联的书 # from app03 import models ret = models.Author.objects.get(id=1).book.all() print(ret) # 从作者关联的书里面移除id是1的书 # 没有django ORM封装的那些快捷方式,我们要自己亲自修改第三张表 models.Author2Book.objects.get(author_id=1, book_id=1).delete()