Django – query not equal
The simpliest way to retrieve data from tables is take them all. To do this, you can write:
|
1
|
all_entries = Entry.objects.all() |
But, usually, you have to select a subset of data. To reach this goal, you can filter the QuerySet with some conditions. The fastest way is yo use the .filter() method, giving as parameter our filterting conditions. For example:
|
1
|
Entry.objects.filter(date = 2006) |
And.. how we can make a not equal filtering condtion?
Changing ‘=’ with ‘!=’ or ‘<>’, will return error messages. And now? The solution is simple.
First of all, import in out file the library for the Q object:
|
1
|
from django.db.models import Q |
Then, we can include our condition in a Q object. To make this a not equal query, write ‘~’ just before the Q object.
|
1
|
Entry.objects.filter(~Q(date = 2006)) |
In this case, the code will return all entries with date field different from 2006.
Django – query not equal的更多相关文章
- Django Query
Making Qeries 一旦创建了数据模型,Django就会自动为您提供一个数据库抽象API,允许您创建.检索.更新和删除对象.本文档解释了如何使用这个API. The models 一个clas ...
- python3环境搭建(uWSGI+django+nginx+python+MySQL)
1.系统环境,必要知识 #cat /etc/redhat-release CentOS Linux release (Core) #uname -r -.el7.x86_64 暂时关闭防护墙,关闭se ...
- Django调试models输出的SQL语句
django1.3在shell下,调试models变得更为简单了,不用像之前的版本,手工去调用django query,才能打印出之前的代码是执行的什么SQL语句. 1.3开始只需在settings. ...
- Django 调试models 输出的SQL语句 定位查看结果
django 调试models变得更为简单了,不用像之前的版本, 手工去调用django query, 才能打印出之前的代码是执行的什么SQL语句. 1.3开始只需在settings.py里,配置如下 ...
- Lambda动态创建
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 通用查询设计思想(2)- 基于ADO.Net的设计
不少公司用的是ADO.NET的访问方式,估计不少朋友对于sql的拼写真是深恶痛绝,在没有一个封装足够好的底层的项目,特别是经过许多人接手之后,代码那叫一个惨不忍睹,本文借助[通用查询设计思想]这篇文章 ...
- python中的 __repr__和__str__
__repr__,被内置函数repr用于把一个对象用"官方"的字符串形式表示出来(终端友好) 1.值传给eval()来返回一个对象的字符串表示形式 2.否则返回一个尖括 ...
- python代码的那些设计
一.Django的ORM 1.类QuerySet (django) :QuerySet 可以被构造,过滤,切片,做为参数传递,这些行为都不会对数据库进行操作.只要你查询的时候才真正的操作数据库. 2. ...
- EntitySpace 常用语句
EntitySpace 这个是很早期的ORM框架,最近发现这个破解的也都不能用了.有谁知道能用的,联系我. 1. where带几个条件的 query.Where(query.ProductTempSt ...
随机推荐
- 【转】javascript入门系列演示·三种弹出对话框的用法实例
对话框有三种 1:只是提醒,不能对脚本产生任何改变: 2:一般用于确认,返回 true 或者 false ,所以可以轻松用于 if...else...判断 3: 一个带输入的对话框,可以返回用户填入的 ...
- python exec
exec官方声明This statement supports dynamic execution of Python code. exec语句用来执行储存在字符串或文件中的python语句.exec ...
- json字符串转map
<dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</ar ...
- mysql edit
表外键5个相关性: cascade,restrict,set null,no action,default show character set ; show collation like ' ...
- 如何进行oracle capability i/o(压力测试数据库服务器i/o性能)
一 .oracle 有关 IO 信息的相关统计函数 Oracle i/o stack包含hbas,存储相关的交换机.存储物理磁盘.那么oracle建议在应用程序部署的时候,建议去验证i/o避免存在问题 ...
- 远程访问linux环境安装图形界面问题解决汇总
本文内容转摘于其他网页,仅用于学习: 通常Linux出现 DISPLAY 尚未设置 解决方法,在root用户目录下执行#xhost +: [root@TEST144239 ~]# xhost + ac ...
- 深入理解HashMap
转自:http://annegu.iteye.com/blog/539465 Hashmap是一种非常常用的.应用广泛的数据类型,最近研究到相关的内容,就正好复习一下.网上关于hashmap的文章很多 ...
- EXECUTE 后的事务计数指示 BEGIN 和 COMMIT 语句的数目不匹配
代码中的事务包含了存储过程中的事务.
- KindEditor得不到textarea值的解决方法----摘至天涯
以前有朋友遇到过这个问题,就是KindEditor在火狐下或者其他浏览器下都无法得到textarea文本框的值,点击表单提交按钮得到的是空白.昨天天涯PHP博客[http://blog.phpha.c ...
- 黄聪:wordpress/wp-includes目录文件
wp-includes/cache.php wp-includes/capabilities.php wp-includes/class-IXR.php:Incutio XML-RPC库.包括了 XM ...