<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Dynamic Django queries (or why kwargs is your friend)</title>
	<atom:link href="http://www.nomadjourney.com/2009/04/dynamic-django-queries-with-kwargs/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.nomadjourney.com/2009/04/dynamic-django-queries-with-kwargs/</link>
	<description>Currently in transit between here and there...</description>
	<lastBuildDate>Wed, 30 Nov 2011 13:31:00 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: Mansoorulhaq</title>
		<link>http://www.nomadjourney.com/2009/04/dynamic-django-queries-with-kwargs/comment-page-1/#comment-620</link>
		<dc:creator>Mansoorulhaq</dc:creator>
		<pubDate>Wed, 27 Apr 2011 17:50:26 +0000</pubDate>
		<guid isPermaLink="false">http://www.nomadjourney.com/?p=190#comment-620</guid>
		<description>vary helpful information. I was looking for.  thanks</description>
		<content:encoded><![CDATA[<p>vary helpful information. I was looking for.  thanks</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: airstrike</title>
		<link>http://www.nomadjourney.com/2009/04/dynamic-django-queries-with-kwargs/comment-page-1/#comment-568</link>
		<dc:creator>airstrike</dc:creator>
		<pubDate>Fri, 04 Mar 2011 14:41:33 +0000</pubDate>
		<guid isPermaLink="false">http://www.nomadjourney.com/?p=190#comment-568</guid>
		<description>Any chance you guys could share some of that code? I&#039;m going to start working on something rather similar right now and I&#039;d love to have something of a starting point instead of diving in empty handed. Thanks in advance!</description>
		<content:encoded><![CDATA[<p>Any chance you guys could share some of that code? I&#8217;m going to start working on something rather similar right now and I&#8217;d love to have something of a starting point instead of diving in empty handed. Thanks in advance!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nizam</title>
		<link>http://www.nomadjourney.com/2009/04/dynamic-django-queries-with-kwargs/comment-page-1/#comment-442</link>
		<dc:creator>Nizam</dc:creator>
		<pubDate>Wed, 10 Nov 2010 22:46:25 +0000</pubDate>
		<guid isPermaLink="false">http://www.nomadjourney.com/?p=190#comment-442</guid>
		<description>I am doing it the exact way they describe it in the Django docs:
http://www.djangoproject.com/documentation/models/or_lookups/</description>
		<content:encoded><![CDATA[<p>I am doing it the exact way they describe it in the Django docs:<br />
<a href="http://www.djangoproject.com/documentation/models/or_lookups/" rel="nofollow">http://www.djangoproject.com/documentation/models/or_lookups/</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Renato Pedigoni</title>
		<link>http://www.nomadjourney.com/2009/04/dynamic-django-queries-with-kwargs/comment-page-1/#comment-432</link>
		<dc:creator>Renato Pedigoni</dc:creator>
		<pubDate>Thu, 16 Sep 2010 04:52:44 +0000</pubDate>
		<guid isPermaLink="false">http://www.nomadjourney.com/?p=190#comment-432</guid>
		<description>&quot;&#124;&quot; is the bitwise or operator, its actual function is operator.or_(a, b) [1]

You may want something like this:

import operator
field_lookups = (&#039;title__icontains&#039;, &#039;author__icontains&#039;,)
q_list = [Q(**{f:q}) for f in field_lookups]
Model.objects.filter(reduce(operator.or_, q_list))

[1] http://docs.python.org/library/operator.html#operator.or_</description>
		<content:encoded><![CDATA[<p>&#8220;|&#8221; is the bitwise or operator, its actual function is operator.or_(a, b) [1]</p>
<p>You may want something like this:</p>
<p>import operator<br />
field_lookups = (&#8216;title__icontains&#8217;, &#8216;author__icontains&#8217;,)<br />
q_list = [Q(**{f:q}) for f in field_lookups]<br />
Model.objects.filter(reduce(operator.or_, q_list))</p>
<p>[1] <a href="http://docs.python.org/library/operator.html#operator.or_" rel="nofollow">http://docs.python.org/library/operator.html#operator.or_</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: mike</title>
		<link>http://www.nomadjourney.com/2009/04/dynamic-django-queries-with-kwargs/comment-page-1/#comment-422</link>
		<dc:creator>mike</dc:creator>
		<pubDate>Tue, 20 Jul 2010 12:33:25 +0000</pubDate>
		<guid isPermaLink="false">http://www.nomadjourney.com/?p=190#comment-422</guid>
		<description>thanks for the post, it helped me clarify a few things!</description>
		<content:encoded><![CDATA[<p>thanks for the post, it helped me clarify a few things!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: PhilGo20</title>
		<link>http://www.nomadjourney.com/2009/04/dynamic-django-queries-with-kwargs/comment-page-1/#comment-407</link>
		<dc:creator>PhilGo20</dc:creator>
		<pubDate>Fri, 02 Apr 2010 16:44:51 +0000</pubDate>
		<guid isPermaLink="false">http://www.nomadjourney.com/?p=190#comment-407</guid>
		<description>Thanks for the example, was also trying to use dynamic Q objects. great stuff</description>
		<content:encoded><![CDATA[<p>Thanks for the example, was also trying to use dynamic Q objects. great stuff</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Vladimir Dronnikov</title>
		<link>http://www.nomadjourney.com/2009/04/dynamic-django-queries-with-kwargs/comment-page-1/#comment-383</link>
		<dc:creator>Vladimir Dronnikov</dc:creator>
		<pubDate>Tue, 22 Dec 2009 09:20:37 +0000</pubDate>
		<guid isPermaLink="false">http://www.nomadjourney.com/?p=190#comment-383</guid>
		<description>Very helpful, thanks!

One remark:

args = ( Q( title__icontains = &#039;Foo&#039; ) &#124; Q( title__icontains = &#039;Bar&#039; ) )
in the example should read
args = ( Q( title__icontains = &#039;Foo&#039; ) &#124; Q( title__icontains = &#039;Bar&#039; ) , )
or *args fails to unpack. Notice trailing comma.

Regards,
--
Vladimir</description>
		<content:encoded><![CDATA[<p>Very helpful, thanks!</p>
<p>One remark:</p>
<p>args = ( Q( title__icontains = &#8216;Foo&#8217; ) | Q( title__icontains = &#8216;Bar&#8217; ) )<br />
in the example should read<br />
args = ( Q( title__icontains = &#8216;Foo&#8217; ) | Q( title__icontains = &#8216;Bar&#8217; ) , )<br />
or *args fails to unpack. Notice trailing comma.</p>
<p>Regards,<br />
&#8211;<br />
Vladimir</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nizam</title>
		<link>http://www.nomadjourney.com/2009/04/dynamic-django-queries-with-kwargs/comment-page-1/#comment-110</link>
		<dc:creator>Nizam</dc:creator>
		<pubDate>Tue, 21 Jul 2009 21:28:48 +0000</pubDate>
		<guid isPermaLink="false">http://www.nomadjourney.com/?p=190#comment-110</guid>
		<description>Brooks,

You should be able to just create your Q objects and then OR them together in a list before you pass them to .filter() as follows:

&lt;code&gt;
args = []
q1 = Q( field = blah, field2 = blahblah )
q2 = Q( field3 = blah, field4 = blahblah )
args.append( q1 &#124; q2  )
MyModel.objects.filter( *args )
&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>Brooks,</p>
<p>You should be able to just create your Q objects and then OR them together in a list before you pass them to .filter() as follows:</p>
<p><code><br />
args = []<br />
q1 = Q( field = blah, field2 = blahblah )<br />
q2 = Q( field3 = blah, field4 = blahblah )<br />
args.append( q1 | q2  )<br />
MyModel.objects.filter( *args )<br />
</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brooks Travis</title>
		<link>http://www.nomadjourney.com/2009/04/dynamic-django-queries-with-kwargs/comment-page-1/#comment-108</link>
		<dc:creator>Brooks Travis</dc:creator>
		<pubDate>Fri, 17 Jul 2009 16:23:57 +0000</pubDate>
		<guid isPermaLink="false">http://www.nomadjourney.com/?p=190#comment-108</guid>
		<description>What about constructing dynamic Q objects and &#039;OR&#039;ing them together dynamically? The only thing I&#039;ve been able to come up with is creating the statement as a string:

&#039;Q(field=blah,field2=blahblah)&#124;Q(field3=blah,field4=blahblah)&#039;

But I can&#039;t seem to find a way to plug that in to the filter(). I&#039;ve tried eval(), exec(), and compile(), but no luck. Ideas?</description>
		<content:encoded><![CDATA[<p>What about constructing dynamic Q objects and &#8216;OR&#8217;ing them together dynamically? The only thing I&#8217;ve been able to come up with is creating the statement as a string:</p>
<p>&#8216;Q(field=blah,field2=blahblah)|Q(field3=blah,field4=blahblah)&#8217;</p>
<p>But I can&#8217;t seem to find a way to plug that in to the filter(). I&#8217;ve tried eval(), exec(), and compile(), but no luck. Ideas?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Vinicius Vacanti</title>
		<link>http://www.nomadjourney.com/2009/04/dynamic-django-queries-with-kwargs/comment-page-1/#comment-106</link>
		<dc:creator>Vinicius Vacanti</dc:creator>
		<pubDate>Thu, 09 Jul 2009 23:48:15 +0000</pubDate>
		<guid isPermaLink="false">http://www.nomadjourney.com/?p=190#comment-106</guid>
		<description>@Vinicius

Your comment was very helpful.  I was looking for a way to dynamically create Q objects and you provided the answer!

Also, great name!</description>
		<content:encoded><![CDATA[<p>@Vinicius</p>
<p>Your comment was very helpful.  I was looking for a way to dynamically create Q objects and you provided the answer!</p>
<p>Also, great name!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Vinicius Mendes</title>
		<link>http://www.nomadjourney.com/2009/04/dynamic-django-queries-with-kwargs/comment-page-1/#comment-84</link>
		<dc:creator>Vinicius Mendes</dc:creator>
		<pubDate>Wed, 27 May 2009 11:03:47 +0000</pubDate>
		<guid isPermaLink="false">http://www.nomadjourney.com/?p=190#comment-84</guid>
		<description>Very good approach. I use this in my search engine. Very useful. Just to complete what you said, you can use the same approach with the Q objects. For example:

&lt;code&gt;
kwargs_author = {&#039;author__icontains&#039;: &#039;Vinicius&#039;}
kwargs_title = {&#039;title__icontains&#039;: &#039;Vinicius&#039;}
Entry.objects.filter(Q(**kwargs_author) &#124; Q(**kwargs_title))
&lt;/code&gt;

In my case, I have a form field for each kind of search (integer, date, string, boolean, primary key, choice field). I have a search form also. So, I populate my form with the GET data and it uses a field&#039;s method called get_criteria wich returns a dictionary with in this format (&#039;lookup_method&#039;: &#039;value&#039;). I pass the search string from the GET data to the get_criteria of each field and then get the dictionary returned, and pass to the Q object.</description>
		<content:encoded><![CDATA[<p>Very good approach. I use this in my search engine. Very useful. Just to complete what you said, you can use the same approach with the Q objects. For example:</p>
<p><code><br />
kwargs_author = {'author__icontains': 'Vinicius'}<br />
kwargs_title = {'title__icontains': 'Vinicius'}<br />
Entry.objects.filter(Q(**kwargs_author) | Q(**kwargs_title))<br />
</code></p>
<p>In my case, I have a form field for each kind of search (integer, date, string, boolean, primary key, choice field). I have a search form also. So, I populate my form with the GET data and it uses a field&#8217;s method called get_criteria wich returns a dictionary with in this format (&#8216;lookup_method&#8217;: &#8216;value&#8217;). I pass the search string from the GET data to the get_criteria of each field and then get the dictionary returned, and pass to the Q object.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nizam</title>
		<link>http://www.nomadjourney.com/2009/04/dynamic-django-queries-with-kwargs/comment-page-1/#comment-40</link>
		<dc:creator>Nizam</dc:creator>
		<pubDate>Tue, 28 Apr 2009 14:52:43 +0000</pubDate>
		<guid isPermaLink="false">http://www.nomadjourney.com/?p=190#comment-40</guid>
		<description>Ayaz,

You&#039;re right. Q objects are really handy especially for cases where OR-ing is required. Interestingly, it turns out that QuerySet&#039;s filter method accepts both args and kwargs. So it is possible to mix and match both Q objects and standard filter parameters together. E.g.,

&lt;code&gt;
kwargs = { &#039;deleted_datetime__isnull&#039;: True }
args = ( Q(  title__icontains = &#039;Foo&#039; ) &#124; Q( title__icontains = &#039;Bar&#039; ) )
entries = Entry.objects.filter( *args, **kwargs )
&lt;/code&gt;

I am going to update this post to reflect this fact.</description>
		<content:encoded><![CDATA[<p>Ayaz,</p>
<p>You&#8217;re right. Q objects are really handy especially for cases where OR-ing is required. Interestingly, it turns out that QuerySet&#8217;s filter method accepts both args and kwargs. So it is possible to mix and match both Q objects and standard filter parameters together. E.g.,</p>
<p><code><br />
kwargs = { 'deleted_datetime__isnull': True }<br />
args = ( Q(  title__icontains = 'Foo' ) | Q( title__icontains = 'Bar' ) )<br />
entries = Entry.objects.filter( *args, **kwargs )<br />
</code></p>
<p>I am going to update this post to reflect this fact.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ayaz</title>
		<link>http://www.nomadjourney.com/2009/04/dynamic-django-queries-with-kwargs/comment-page-1/#comment-37</link>
		<dc:creator>ayaz</dc:creator>
		<pubDate>Tue, 28 Apr 2009 04:35:37 +0000</pubDate>
		<guid isPermaLink="false">http://www.nomadjourney.com/?p=190#comment-37</guid>
		<description>Neat one. Though, there&#039;s one constraint that I can see with that approach: you could only specify filter parameters that you want AND&#039;d together (though, that&#039;s the default anyway, yeah, and that&#039;s what we usually want most of the time). When I have to build a filter parameter list in steps, I make great use of Q objects and create Q sets that merge into a bigger Q set that is used at the end. Also, I would love to exploit the lazy nature of QuerySet objects more often.</description>
		<content:encoded><![CDATA[<p>Neat one. Though, there&#8217;s one constraint that I can see with that approach: you could only specify filter parameters that you want AND&#8217;d together (though, that&#8217;s the default anyway, yeah, and that&#8217;s what we usually want most of the time). When I have to build a filter parameter list in steps, I make great use of Q objects and create Q sets that merge into a bigger Q set that is used at the end. Also, I would love to exploit the lazy nature of QuerySet objects more often.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nizam</title>
		<link>http://www.nomadjourney.com/2009/04/dynamic-django-queries-with-kwargs/comment-page-1/#comment-35</link>
		<dc:creator>Nizam</dc:creator>
		<pubDate>Mon, 27 Apr 2009 23:57:39 +0000</pubDate>
		<guid isPermaLink="false">http://www.nomadjourney.com/?p=190#comment-35</guid>
		<description>Its true that QuerySets are lazy but then again, so am I. ;)

I like the kwargs approach a little more since it involves less keystrokes, is slightly more readable (a matter of personal taste) and the end result is essentially the same.

Must reads for anyone wondering how QuerySets work:
http://docs.djangoproject.com/en/dev/topics/db/queries/#id3
http://docs.djangoproject.com/en/dev/ref/models/querysets/#when-querysets-are-evaluated</description>
		<content:encoded><![CDATA[<p>Its true that QuerySets are lazy but then again, so am I. ;)</p>
<p>I like the kwargs approach a little more since it involves less keystrokes, is slightly more readable (a matter of personal taste) and the end result is essentially the same.</p>
<p>Must reads for anyone wondering how QuerySets work:<br />
<a href="http://docs.djangoproject.com/en/dev/topics/db/queries/#id3" rel="nofollow">http://docs.djangoproject.com/en/dev/topics/db/queries/#id3</a><br />
<a href="http://docs.djangoproject.com/en/dev/ref/models/querysets/#when-querysets-are-evaluated" rel="nofollow">http://docs.djangoproject.com/en/dev/ref/models/querysets/#when-querysets-are-evaluated</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Daniel Swarbrick</title>
		<link>http://www.nomadjourney.com/2009/04/dynamic-django-queries-with-kwargs/comment-page-1/#comment-33</link>
		<dc:creator>Daniel Swarbrick</dc:creator>
		<pubDate>Mon, 27 Apr 2009 22:36:10 +0000</pubDate>
		<guid isPermaLink="false">http://www.nomadjourney.com/?p=190#comment-33</guid>
		<description>Django queries are chainable anyway, so you could just do:

&lt;code&gt;
# start with everything, or a subset with Entry.objects.filter(foo) if you like
entries = Entry.objects.all()
&lt;/code&gt;
&lt;code&gt;
if exclude_deleted:
    entries = entries.filter(deleted_datetime__isnull=True)
&lt;/code&gt;
&lt;code&gt;
if category is not None:
    entries = entries.filter(category=category)
&lt;/code&gt;
&lt;code&gt;
if current_user_only:
    entries = entries.filter(user=request.user)
&lt;/code&gt;
&lt;code&gt;
if title_search_query != &#039;&#039;:
    entries = entries.filter(title__icontains=title_search_query)
&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>Django queries are chainable anyway, so you could just do:</p>
<p><code><br />
# start with everything, or a subset with Entry.objects.filter(foo) if you like<br />
entries = Entry.objects.all()<br />
</code><br />
<code><br />
if exclude_deleted:<br />
    entries = entries.filter(deleted_datetime__isnull=True)<br />
</code><br />
<code><br />
if category is not None:<br />
    entries = entries.filter(category=category)<br />
</code><br />
<code><br />
if current_user_only:<br />
    entries = entries.filter(user=request.user)<br />
</code><br />
<code><br />
if title_search_query != '':<br />
    entries = entries.filter(title__icontains=title_search_query)<br />
</code></p>
]]></content:encoded>
	</item>
</channel>
</rss>

