9.30 博客项目迭代1.3
2018-9-30 18:45:02
迭代了自己博客项目
放上github : https://github.com/TrueNewBee/bbs_demo
明天周一 十一 已经没啥概念啦!对放假
明天继续迭代自己的博客项目!! 然后把Django复习一下 整理一下笔记!
博客项目还是蛮成功的,就是就有点丑!可以优化!!!
每次一的优化,都是值得期待的!
越努力,越幸运!永远不要高估自己!
ORM分组实例
import os if __name__ == '__main__': os.environ.setdefault("DJANGO_SETTINGS_MODULE", "orm_demo.settings") import django django.setup() from app01 import models # ORM分组查询 每个部门名称及部门的平均工资 # ret = models.Employee.objects.all() # """ # SELECT `employee`.`id`, `employee`.`name`, `employee`.`age`, `employee`.`salary`, `employee`.`province`, `employee`.`dept` FROM `employee` LIMIT 21; args=() # """ # print(ret) # ret = models.Employee.objects.all().values("dept", "age") # """ # SELECT `employee`.`dept`, `employee`.`age` FROM `employee` LIMIT 21; args=() # """ # print(ret) from django.db.models import Avg # ret = models.Employee.objects.values("province").annotate(a=Avg("salary")).values("province", "a") # """ # SELECT `employee`.`province`, AVG(`employee`.`salary`) AS `a` FROM `employee` GROUP BY `employee`.`province` ORDER BY NULL LIMIT 21; args=() # """ # print(ret) # ORM连表分组查询 # ret = models.Person.objects.values("dept_id").annotate(a=Avg("salary")).values("dept__name", "a") # """ # SELECT `dept`.`name`, AVG(`person`.`salary`) AS `a` FROM `person` INNER JOIN `dept` ON (`person`.`dept_id` = `dept`.`id`) GROUP BY `person`.`dept_id`, `dept`.`name` ORDER BY NULL LIMIT 21; args=() # """ # print(ret) # 查询person表,判断每个人的工资是否大于2000 # ret = models.Person.objects.all().extra( # select={"gt": "salary > 2000"} # ) # # """ # SELECT (salary > 2000) AS `gt`, `person`.`id`, `person`.`name`, `person`.`salary`, `person`.`dept_id` FROM `person` LIMIT 21; args=() # """ # # print(ret) # for i in ret: # print(i.name, i.gt) # 执行原生的SQL语句 from django.db import connection cursor = connection.cursor() # 获取光标,等待执行SQL语句 cursor.execute("""SELECT * from person where id = %s""", [1]) row = cursor.fetchone() print(row)
日期归档查询和部分其他查询示例
import os if __name__ == '__main__': os.environ.setdefault("DJANGO_SETTINGS_MODULE", "bbs.settings") import django django.setup() from blog import models # 查询a1对应的评论数 # ret = models.Article.objects.first().comment_set.all() # print(ret) # 查询某个分类对应的文章 from django.db.models import Count user = models.UserInfo.objects.filter(username="xiaohei").first() blog = user.blog # ret = models.Category.objects.filter(blog=blog) # 求小黑站点下面所有的文章分类 # ret = ret[0].article_set.all() # 技术分类下面所有的文章 # for i in ret: # print(i.title, i.article_set.all().count()) # ret = models.Category.objects.filter(blog=blog).annotate(c=Count("article")).values("title", "c") # print(ret) # 基于QuerySet查询的时候 不用加set # models.Category.objects.filter(blog=blog).values("article__title") ret = models.Article.objects.filter(user=user).extra( select={"archive_ym": "date_format(create_time,'%%Y-%%m')"} ).values("archive_ym").annotate(c=Count("nid")).values("archive_ym", "c") print(ret)
笔记
day78 2018-05-25 1. 复习下分组和聚合 https://www.cnblogs.com/liwenzhou/p/8660826.html 1. 分组 ORM中values或者values_list 里面写什么字段,就相当于select 什么字段 ret = models.Employee.objects.all().values("dept", "age") 相当于: SELECT `employee`.`dept`, `employee`.`age` FROM `employee` LIMIT 21; args=() 2. ORM中 annotate 前面是什么就按照什么分组! from django.db.models import Avg ret = models.Employee.objects.values("province").annotate(a=Avg("salary")).values("province", "a") 相当于: SELECT `employee`.`province`, AVG(`employee`.`salary`) AS `a` FROM `employee` GROUP BY `employee`.`province` ORDER BY NULL LIMIT 21; args=() 3. extra --> 在执行ORM查询的时候执行额外的SQL语句 # 查询person表,判断每个人的工资是否大于2000 ret = models.Person.objects.all().extra( select={"gt": "salary > 2000"} ) 相当于: SELECT (salary > 2000) AS `gt`, `person`.`id`, `person`.`name`, `person`.`salary`, `person`.`dept_id` FROM `person` LIMIT 21; args=() 4. 直接执行原生的SQL语句,类似pymysql的用法 from django.db import connection cursor = connection.cursor() # 获取光标,等待执行SQL语句 cursor.execute("""SELECT * from person where id = %s""", [1]) row = cursor.fetchone() print(row) 2. 个人站点首页 - 分组和聚合查询 1. 文章分类 2. 标签分类 3. 日期归档 1. MySQL内置的方法:date_format(字段名, "%Y-%m") 2. extra() --> 执行额外的原生SQL语句 ret = models.Article.objects.filter(user=user).extra( select={"archive_ym": "date_format(create_time,'%%Y-%%m')"} ).values("archive_ym").annotate(c=Count("nid")).values("archive_ym", "c")