Web框架开发-Django-extra过滤

extra

|-----|----------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 | extra(select``=``None``, where``=``None``, params``=``None``, ``tables``=``None``, order_by``=``None``, select_params``=``None``) |

  

有些情况下,Django的查询语法难以简单的表达复杂的 WHERE 子句,对于这种情况, Django 提供了 extra() QuerySet修改机制 --- 它能在 QuerySet生成的SQL从句中注入新子句

extra可以指定一个或多个 参数,例如 select, where or tables. 这些参数都不是必须的,但是你至少要使用一个!要注意这些额外的方式对不同的数据库引擎可能存在移植性问题.(因为你在显式的书写SQL语句),除非万不得已,尽量避免这样做

参数之select

The select 参数可以让你在 SELECT 从句中添加其他字段信息,它应该是一个字典,存放着属性名到 SQL 从句的映射。

复制代码
queryResult=models.Article
           .objects.extra(select={'is_recent': "create_time > '2017-09-05'"})

结果集中每个 Entry 对象都有一个额外的属性is_recent, 它是一个布尔值,表示 Article对象的create_time 是否晚于2017-09-05.

练习:

|---------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 6 7 | # in sqlite: ``article_obj``=``models.Article.objects               .``filter``(nid``=``1``)               .extra(select``=``{``"standard_time"``:``"strftime('%%Y-%%m-%%d',create_time)"``})               .values(``"standard_time"``,``"nid"``,``"title"``) ``print``(article_obj) ``# <QuerySet [{'title': 'MongoDb 入门教程', 'standard_time': '2017-09-03', 'nid': 1}]> |

 

参数之where / tables

您可以使用where定义显式SQL WHERE子句 - 也许执行非显式连接。您可以使用tables手动将表添加到SQL FROM子句。

where和tables都接受字符串列表。所有where参数均为"与"任何其他搜索条件。

举例来讲""

复制代码
queryResult=models.Article
           .objects.extra(where=['nid in (1,3) OR title like "py%" ','nid>2'])

 举例:

|----------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 6 7 8 9 10 | current_user ``= models.UserInfo.objects.``filter``(username``=``username).first() ``#当前用户 【每一步的分析过程】 1``、models.Article.objects.``all``() ``#查出每一篇文章 2``、models.Article.objects.``all``().``filter``(user``=``current_user) ``#查出当前用户的所有文章 3``、models.Article.objects.``all``().``filter``(user``=``current_user).extra(select``=``{``"filter_create_date"``:``"strftime('%%Y/%%m',create_time)"``}).values_list(``"filter_create_date"``) #查出当前用户的所有文章的create_time,并且只取出年份和月份 解决方案:使用extra方法 extra使用来进行过滤的,参数select必须等于一个字典(转成sql的where语句去执行,查出create_time,然后转换成自己格式化的时间) 4``、models.Article.objects.``all``().``filter``(user``=``current_user).extra(select``=``{``"filter_create_date"``:``"strftime('%%Y/%%m',create_time)"``}).values_list(``"filter_create_date"``).annotate(Count(``"title"``)) #按照查询出来的年份和月份进行分组,并且显示文章个数 |

相关推荐
七牛开发者4 小时前
为什么 Go 很适合 AI 辅助开发?
数据库·人工智能·python·elasticsearch·log4j
Eason_Lou5 小时前
【无标题】
前端·vue.js·html
_codemonster5 小时前
npm run dev 是在开发模式运行,怎么在生产环境运行
前端·npm·node.js
kyriewen5 小时前
我受够了复制报错去问 AI,花一下午给控制台做了个调试助手
前端·javascript·ai编程
何以解忧,唯有..6 小时前
数据库索引失效的常见情况与优化策略
数据库·sql·oracle
IT_陈寒6 小时前
Redis的DEL命令竟然没删掉数据?我踩的这个坑你得知道
前端·人工智能·后端
anOnion6 小时前
构建无障碍组件之Menu and Menubar Pattern
前端·html·交互设计
JavaGuide7 小时前
我用 Claude Code/ZCode+GLM-5.3 从零做了一款 Agent 游戏!
前端·后端
Eason_Lou7 小时前
vue flow使用注意事项
前端·vue.js
这个DBA有点耶7 小时前
分布式数据库到底该不该上?从判断标准到架构选型的实战思考
数据库·架构·dba