Django使用distinct报错:DISTINCT ON fields is not supported by this database backend
具体错误提示是:django.db.utils.NotSupportedError: DISTINCT ON fields is not supported by this database backend
刚好遇到mysql数据库查重,使用distinct方法来做去重查询,结果遇到这个错误。先看一下distinct的源码:
def distinct(self, *field_names):
"""
Return a new QuerySet instance that will select only distinct results.
"""
assert self.query.can_filter(), \
"Cannot create distinct fields once a slice has been taken."
obj = self._chain()
obj.query.add_distinct_fields(*field_names)
return obj
使用distinct会返回一个新的查询集合,会显示不重复的结果,达到去重的目的。我们可以看到它接受的是一个或多个field_names参数,也就是各字段名称。
我写的错误写法:u.objects.all().distinct('address')。

这是报的错误。。。根据提示说明distinct不支持当前的数据库。
我们可以去官网看一下这个字段的说明:https://docs.djangoproject.com/en/2.2/ref/models/querysets/#distinct
官网提到了上面这种错误的写法,在mysql中是不支持的,支持postgresql,也就是distinct()里面不能指定字段。
错误解决
不能这样,那我们可以把数据写到values或者values_list里面去不就好了。
查询语句如下:
u.objects.filter(user_id=1).values('address', "signer_name", 'signer_mobile').distinct()
输出结果如下:
<QuerySet [{'address': '188号蓝天国际大厦', 'signer_name': 'admin', 'signer_mobile': ''}, {'address': '中原路188号', 'signer_name': '牛', 'signer_mobile': ''}, {'address': '南关街14号', 'signer_name': '刘雨辰', 'signer_mobile': ''}, {'address': '麓山南路麓山门', 'signer_name': '湖南大学', 'signer_mobile': ''}, {'address': '安徽省合肥市金寨路 96 号', 'signer_name': '中国科学技术大学', 'signer_mobile': ''}]>
对应于SQL语句:
SELECT DISTINCT address, signer_name, signer_mobile FROM user_operation_useraddress WHERE user_id=1; // 去重查询语句 SELECT COUNT(*) as repetitions, address, signer_name, signer_mobile FROM user_operation_useraddress GROUP BY address, signer_name, signer_mobile; // 去重查询以及字段数量统计 SELECT COUNT(*) as repetitions, address, signer_name, signer_mobile FROM user_operation_useraddress GROUP BY address, signer_name, signer_mobile HAVING repetitions > 1; // 去重查询,字段记录数量统计大于1条的
以上就是mysql环境下,django的去重查询方式。
Django使用distinct报错:DISTINCT ON fields is not supported by this database backend的更多相关文章
- django startproject xxx:报错UnicodeDecodeError: 'ascii' codec can't decode byte 0xe6 in position 13: ordinal not in range(128)
django startproject xxx:报错UnicodeDecodeError: 'ascii' codec can't decode byte 0xe6 in position 13: o ...
- Django迁移数据库报错
Django迁移数据库报错 table "xxx" already exists错误 django在migrate时报错django migrate error: table 'x ...
- django正常运行确报错的解决方法
django正常运行却报错的处理方法 出处 : https://www.infvie.com/ops-notes/django-normal-operation-error 报错一:self._soc ...
- AS报错:lambda expressions are not supported at this language level
AS报错:lambda expressions are not supported at this language level 解决方法 打开打开 File --> Project Stuct ...
- mycat 1.6.6.1 distinct报错问题
以前在mysql5.7上执行如下sql语句没有问题 SELECT DISTINCT u.*,c.content userCategory FROM m_user u LEFT JOIN m_categ ...
- Django:django-cors-headers 报错no module named "corsheaders"
django跨域使用 pip install django-cors-headers 然后在settings文件中加上参数设置 # app配置 INSTALLED_APPS = [ 'django.c ...
- django调用py报错 django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, but settings are not configured.
完整报错信息如下 django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, bu ...
- django 连接mysql报错
原因: 问题1. 即从mysql5.7版本之后,默认采用了caching_sha2_password验证方式. 问题2. 然后在执行 python manage.py makemigrations依 ...
- Django创建App报错
在django下创建APP项目时遇到的坑 python manage.py startapp app01 报错内容如下: 解决:找到报错中的文件夹151行删除items(),)中的逗号即可 在命令行下 ...
随机推荐
- v-model和 .sync
1.v-model的双向数据绑定其实是一个语法糖,类似于,给子组件传入一个value值,并且监听子组件的input事件,在这个事件里将子组件传过来的新值赋值给父组件的value <Input v ...
- 自动创建Kibana索引
参考 https://www.cnblogs.com/dance-walter/p/10471950.html 参考 https://www.elastic.co/guide/en/kibana/cu ...
- Centos 7.x卸载ibus黑屏修复及fcitx搜狗拼音安装方法
ibus黑屏修复 百度出来的fcitx安装方法,都要卸载ibus,如果没有注意同时卸载掉的依赖包的话,gnome桌面中的一些关键库也没被卸载. 修复方法很简单,重新安装Gnome sudo yum - ...
- Android相关视频
Android架构师 层次分析 –从顶层到底层 洞察其原理https://www.bilibili.com/video/av59066641?t=132安卓/Android 逆向破解系统班 第2期 全 ...
- android studio 创建项目的一些配置
build.gradle文件 apply plugin: 'com.android.application' apply plugin: 'org.greenrobot.greendao' // 使用 ...
- 【C++】C++中explicity关键字的使用
读者可以尝试预言一下这段代码的输出: #include <iostream> using namespace std; class Complex { private: double re ...
- 破解NFC卡
目录 概念 各种卡 IC卡存储器结构 破解工具 破解NFC卡 概念 各种卡 ID卡 工作在低频(125Khz) ID卡 特点 EM4XX系列,多为EM4100/EM4102卡 常用的固化ID卡,出厂固 ...
- C#-DllImport 路径问题
原文:C# DllImport 相对路径无法找到dll DllImport DLL查找顺序:1.应用程序所在目录2.Windows目录和Windows\System32目录3.环境变量目录 只需要你把 ...
- RocketMQ安装部署
一.简介RocketMQ RocektMQ是阿里巴巴在2012年开源的一个纯java.分布式.队列模型的第三代消息中间件,不仅在传统高频交易链路有着低延迟的出色表现,在实时计算等大数据领域也有着不错的 ...
- Mac安装MySQL-python的血泪史
现象描述 起初正常使用pip命令提示如下的错误: cc -bundle -undefined dynamic_lookup -Wl,-F. build/temp.macosx-10.14-intel- ...