码迷,mamicode.com
首页 > 其他好文 > 详细

Django – query not equal

时间:2016-05-06 20:24:05      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:

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

标签:

原文地址:http://www.cnblogs.com/ymy124/p/5466844.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!