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 ...
随机推荐
- maxscript, 数组和字符串下标是从1开始的
maxscript中数组和字符串下标是从1开始的.
- Q7: Unique Binary Search Trees
问题描述: Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For ...
- java8新语法
Lambda表达式和函数接口(Functional Interface) // lambda expressions public static void DemoLambda() { // 不使用l ...
- Splashscreen
Splashscreen Enables developers to show/hide the application's splash screen. Methods show hide Perm ...
- Struts2 - 常用的constant总结
见注释 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC &quo ...
- dismissViewControllerAnimated有延迟
dismissViewControllerAnimated:completion:在应用中运行正常,就是不知道为什么出现了几秒钟的延迟: [api loginWithUsername:[dict ob ...
- jquery 整理之一
1.选择器: jQuery 属性选择器 jQuery 使用 XPath 表达式来选择带有给定属性的元素. $("[href]") 选取所有带有 href 属性的元素. $(&quo ...
- 图像特征提取之(一)HOG特征
1.HOG特征: 方向梯度直方图(Histogram of Oriented Gradient, HOG)特征是一种在计算机视觉和图像处理中用来进行物体检测的特征描述子.它通过计算和统计图像局部区域的 ...
- 基于.net开发chrome核心浏览器
本文转载自:http://www.cnblogs.com/liulun/archive/2013/04/20/3031502.html 一: 上一篇的链接: 基于.net开发chrome核心浏览器[一 ...
- quotas and disk replace on netapp
==================================================================================================== ...