Django中ORM找出内容不为空的数据实例


Posted in Python onMay 20, 2020

在django操作数据库的时候如何找出内容不为空的数据呢?

from django.db.models import Q
 
class Index(VIew):
 def get(self, request):
 userObj = models.Asset.objects.filter(~Q(asset_id = '')
 return HttpResponse('yes')

上面代码中的models.Asset.objects.filter(~Q(nick = '')则是使用Q函数去找出nick不为空的数据,主要使用~Q

补充知识:Django报错 HINT: Add or change a related_name argument to the definition for 'GodownentryReturn.suppl

Unhandled exception in thread started by <function wrapper at 0x05569030>
Traceback (most recent call last):
File “G:\workspace\pycharm\carwin\carwin_env\lib\site-packages\django\utils\autoreload.py”, line 226, in wrapper
fn(*args, **kwargs)
File “G:\workspace\pycharm\carwin\carwin_env\lib\site-packages\django\core\management\commands\runserver.py”, line 116, in inner_run
self.check(display_num_errors=True)
File “G:\workspace\pycharm\carwin\carwin_env\lib\site-packages\django\core\management\base.py”, line 472, in check
raise SystemCheckError(msg)
django.core.management.base.SystemCheckError: SystemCheckError: System check identified some issues:

ERRORS:

article.GodownentryReturn.supplier: (fields.E304) Reverse accessor for ‘GodownentryReturn.supplier' clashes with reverse accessor for ‘GodownentryReturn.supplier'.

HINT: Add or change a related_name argument to the definition for ‘GodownentryReturn.supplier' or ‘GodownentryReturn.supplier'.

part.GodownentryReturn.supplier: (fields.E304) Reverse accessor for ‘GodownentryReturn.supplier' clashes with reverse accessor for ‘GodownentryReturn.supplier'.

HINT: Add or change a related_name argument to the definition for ‘GodownentryReturn.supplier' or ‘GodownentryReturn.supplier'.

原因:

因为在一个表中多次引用某个外键表,而且没有指定唯一的releated_name,而导致的。

解决方案:

将同一个表中外键,增加不同的releated_name。

以上这篇Django中ORM找出内容不为空的数据实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python字符串特性及常用字符串方法的简单笔记
Jan 04 Python
批量获取及验证HTTP代理的Python脚本
Apr 23 Python
python实现简易通讯录修改版
Mar 13 Python
python验证码识别教程之利用滴水算法分割图片
Jun 05 Python
Python使用POP3和SMTP协议收发邮件的示例代码
Apr 16 Python
python实现图片中文字分割效果
Jul 22 Python
python使用paramiko模块通过ssh2协议对交换机进行配置的方法
Jul 25 Python
Django继承自带user表并重写的例子
Nov 18 Python
python 实现多维数组转向量
Nov 30 Python
python 连续不等式语法糖实例
Apr 15 Python
解决安装新版PyQt5、PyQT5-tool后打不开并Designer.exe提示no Qt platform plugin的问题
Apr 24 Python
Python数据相关系数矩阵和热力图轻松实现教程
Jun 16 Python
django为Form生成的label标签添加class方式
May 20 #Python
django orm模块中的 is_delete用法
May 20 #Python
Python3批量创建Crowd用户并分配组
May 20 #Python
解决windows上安装tensorflow时报错,“DLL load failed: 找不到指定的模块”的问题
May 20 #Python
django中related_name的用法说明
May 20 #Python
在django中form的label和verbose name的区别说明
May 20 #Python
win7上tensorflow2.2.0安装成功 引用DLL load failed时找不到指定模块 tensorflow has no attribute xxx 解决方法
May 20 #Python
You might like
PHP学习之正则表达式
2011/04/17 PHP
PHP统计二维数组元素个数的方法
2013/11/12 PHP
php导出excel格式数据问题
2014/03/11 PHP
php里array_work用法实例分析
2015/07/13 PHP
PHP中new static()与new self()的比较
2016/08/19 PHP
Ext 表单布局实例代码
2009/04/30 Javascript
jquery五角星评分插件示例分享
2014/02/21 Javascript
js表单验证实例讲解
2016/03/31 Javascript
BootStrap Typeahead自动补全插件实例代码
2016/08/10 Javascript
javascript中活灵活现的Array对象详解
2016/11/30 Javascript
使用nodejs下载风景壁纸
2017/02/05 NodeJs
js中offset,client , scroll 三大元素知识点总结
2019/09/11 Javascript
Layui弹框中数据表格中可双击选择一条数据的实现
2020/05/06 Javascript
你不知道的SpringBoot与Vue部署解决方案
2020/11/09 Javascript
[01:02]2014 DOTA2国际邀请赛中国区预选赛 现场抢先看
2014/05/22 DOTA
[00:36]DOTA2风云人物相约完美“圣”典 12月17日不见不散
2016/11/30 DOTA
python Django批量导入数据
2016/03/25 Python
Python Requests 基础入门
2016/04/07 Python
Python二叉搜索树与双向链表转换实现方法
2016/04/29 Python
Python格式化输出%s和%d
2018/05/07 Python
pandas 对series和dataframe进行排序的实例
2018/06/09 Python
使用python对excle和json互相转换的示例
2018/10/23 Python
只需7行Python代码玩转微信自动聊天
2019/01/27 Python
CSS3 渐变(Gradients)之CSS3 径向渐变
2016/07/08 HTML / CSS
extern是什么意思
2016/03/10 面试题
自我鉴定200字
2013/10/28 职场文书
小学生竞选班长演讲稿
2014/04/24 职场文书
关于安全的标语
2014/06/10 职场文书
白莲教口号
2014/06/18 职场文书
授权收款委托书
2014/09/23 职场文书
党的群众路线教育实践活动个人批评与自我批评
2014/10/16 职场文书
社区党员群众路线教育实践活动心得体会
2014/11/03 职场文书
破坏寝室公物检讨书
2014/11/17 职场文书
求职自荐信范文(优秀篇)
2015/03/27 职场文书
导游词之苏州阳澄湖
2019/11/15 职场文书
教你怎么用python selenium实现自动化测试
2021/05/27 Python