<?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 OR&#8217;d Queryset creation with Django</title>
	<atom:link href="http://www.elfsternberg.com/2009/08/13/dynamic-ord-queryset-creation-with-django/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.elfsternberg.com/2009/08/13/dynamic-ord-queryset-creation-with-django/</link>
	<description>Done, and gets things smart.</description>
	<lastBuildDate>Fri, 03 Feb 2012 14:39:41 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: Dynamic OR&#8217;d Queryset Creation with Django &#171; SRA Lab</title>
		<link>http://www.elfsternberg.com/2009/08/13/dynamic-ord-queryset-creation-with-django/comment-page-1/#comment-4973</link>
		<dc:creator>Dynamic OR&#8217;d Queryset Creation with Django &#171; SRA Lab</dc:creator>
		<pubDate>Thu, 27 Jan 2011 13:22:30 +0000</pubDate>
		<guid isPermaLink="false">http://www.elfsternberg.com/?p=337#comment-4973</guid>
		<description>[...] complete posts and comments published on Elf Sternberg&#039;s blog, this is the method I found to create dynamic complex queryset with Django (Q and [...]</description>
		<content:encoded><![CDATA[<p>[...] complete posts and comments published on Elf Sternberg&#039;s blog, this is the method I found to create dynamic complex queryset with Django (Q and [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: fayce</title>
		<link>http://www.elfsternberg.com/2009/08/13/dynamic-ord-queryset-creation-with-django/comment-page-1/#comment-791</link>
		<dc:creator>fayce</dc:creator>
		<pubDate>Sat, 07 Nov 2009 22:54:58 +0000</pubDate>
		<guid isPermaLink="false">http://www.elfsternberg.com/?p=337#comment-791</guid>
		<description>I found the solution ! and it works !!

I had to combine Travis Cline method and some dicts and everything works just fine... 

So, if someone is interested to know more about how I did, feel free to ask.</description>
		<content:encoded><![CDATA[<p>I found the solution ! and it works !!</p>
<p>I had to combine Travis Cline method and some dicts and everything works just fine&#8230; </p>
<p>So, if someone is interested to know more about how I did, feel free to ask.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: fayce</title>
		<link>http://www.elfsternberg.com/2009/08/13/dynamic-ord-queryset-creation-with-django/comment-page-1/#comment-790</link>
		<dc:creator>fayce</dc:creator>
		<pubDate>Sat, 07 Nov 2009 19:41:11 +0000</pubDate>
		<guid isPermaLink="false">http://www.elfsternberg.com/?p=337#comment-790</guid>
		<description>Thank you for this post ! all thoses answers were helpful to me and really instructive... but I still can find how I can apply it to my problem even if it&#039;s really similar.

Everything is dynamic in my queryset:
- the logical operator ( &#124; or &amp;)
- the operator (lte, gte, icontains ....)
- the name of the field (what you call the mode?)
- and the value...

I would like to build the complete queryset from strings, like this:

tab = 
[[&#039;field1&#039;,&#039;value1&#039;,&#039;operator1&#039;,&#039;logical-operator1&#039;],
[&#039;field2&#039;,&#039;value2&#039;,&#039;operator2&#039;,&#039;logical-operator2&#039;],
[&#039;field3&#039;,&#039;value3&#039;,&#039;operator3&#039;,&#039;logical-operator3&#039;]
]

for e in tab:
    req = req e[3] Q(e[0]__e[2]=e[1]) 

r = Obj.Objects.filter(req)

I know this is completely wrong and will never work.. but this is the idea..

Thank you for you help !</description>
		<content:encoded><![CDATA[<p>Thank you for this post ! all thoses answers were helpful to me and really instructive&#8230; but I still can find how I can apply it to my problem even if it&#8217;s really similar.</p>
<p>Everything is dynamic in my queryset:<br />
- the logical operator ( | or &amp;)<br />
- the operator (lte, gte, icontains &#8230;.)<br />
- the name of the field (what you call the mode?)<br />
- and the value&#8230;</p>
<p>I would like to build the complete queryset from strings, like this:</p>
<p>tab =<br />
[['field1','value1','operator1','logical-operator1'],<br />
['field2','value2','operator2','logical-operator2'],<br />
['field3','value3','operator3','logical-operator3']<br />
]</p>
<p>for e in tab:<br />
    req = req e[3] Q(e[0]__e[2]=e[1]) </p>
<p>r = Obj.Objects.filter(req)</p>
<p>I know this is completely wrong and will never work.. but this is the idea..</p>
<p>Thank you for you help !</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Elf Sternberg</title>
		<link>http://www.elfsternberg.com/2009/08/13/dynamic-ord-queryset-creation-with-django/comment-page-1/#comment-581</link>
		<dc:creator>Elf Sternberg</dc:creator>
		<pubDate>Fri, 14 Aug 2009 16:49:38 +0000</pubDate>
		<guid isPermaLink="false">http://www.elfsternberg.com/?p=337#comment-581</guid>
		<description>Andre, yes, I know about the plural vs. singular.  It&#039;s correct in the production code; I slipped making the example.  And as I pointed out to Brian, in the production code multiple remote applications are being sent requests for querysets that need to be OR&#039;d for the final production.  Would it work correctly for, say, &quot;contains&quot; as the argument?  (There&#039;s a lot of text string searching going on.)</description>
		<content:encoded><![CDATA[<p>Andre, yes, I know about the plural vs. singular.  It&#8217;s correct in the production code; I slipped making the example.  And as I pointed out to Brian, in the production code multiple remote applications are being sent requests for querysets that need to be OR&#8217;d for the final production.  Would it work correctly for, say, &#8220;contains&#8221; as the argument?  (There&#8217;s a lot of text string searching going on.)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Elf Sternberg</title>
		<link>http://www.elfsternberg.com/2009/08/13/dynamic-ord-queryset-creation-with-django/comment-page-1/#comment-580</link>
		<dc:creator>Elf Sternberg</dc:creator>
		<pubDate>Fri, 14 Aug 2009 16:47:42 +0000</pubDate>
		<guid isPermaLink="false">http://www.elfsternberg.com/?p=337#comment-580</guid>
		<description>Brian, that&#039;s useful under most circumstances, but in actual usage, I was getting querysets generated from multiple, sometimes remote, sources, and needed them OR&#039;d together for the final result.  The snippet I posted was simply for illustrative purposes.</description>
		<content:encoded><![CDATA[<p>Brian, that&#8217;s useful under most circumstances, but in actual usage, I was getting querysets generated from multiple, sometimes remote, sources, and needed them OR&#8217;d together for the final result.  The snippet I posted was simply for illustrative purposes.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brian Luft</title>
		<link>http://www.elfsternberg.com/2009/08/13/dynamic-ord-queryset-creation-with-django/comment-page-1/#comment-579</link>
		<dc:creator>Brian Luft</dc:creator>
		<pubDate>Fri, 14 Aug 2009 16:15:56 +0000</pubDate>
		<guid isPermaLink="false">http://www.elfsternberg.com/?p=337#comment-579</guid>
		<description>While OR&#039;ing Q objects together can be useful in some scenarios (may not scale well), why didn&#039;t you use something like:

Responses.objects.filter(mode__in=modes)</description>
		<content:encoded><![CDATA[<p>While OR&#8217;ing Q objects together can be useful in some scenarios (may not scale well), why didn&#8217;t you use something like:</p>
<p>Responses.objects.filter(mode__in=modes)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Andre LeBlanc</title>
		<link>http://www.elfsternberg.com/2009/08/13/dynamic-ord-queryset-creation-with-django/comment-page-1/#comment-578</link>
		<dc:creator>Andre LeBlanc</dc:creator>
		<pubDate>Fri, 14 Aug 2009 15:10:17 +0000</pubDate>
		<guid isPermaLink="false">http://www.elfsternberg.com/?p=337#comment-578</guid>
		<description>I&#039;m about 90% sure that 

    qset = Responses.objects.filter(mode__in=modes)

would have the exact same effect.

also, naming a model after the plural form of the word is considered bad form. the model really should be called &quot;Response&quot; but thats just nitpicking.</description>
		<content:encoded><![CDATA[<p>I&#8217;m about 90% sure that </p>
<p>    qset = Responses.objects.filter(mode__in=modes)</p>
<p>would have the exact same effect.</p>
<p>also, naming a model after the plural form of the word is considered bad form. the model really should be called &#8220;Response&#8221; but thats just nitpicking.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bartek</title>
		<link>http://www.elfsternberg.com/2009/08/13/dynamic-ord-queryset-creation-with-django/comment-page-1/#comment-577</link>
		<dc:creator>Bartek</dc:creator>
		<pubDate>Fri, 14 Aug 2009 14:14:08 +0000</pubDate>
		<guid isPermaLink="false">http://www.elfsternberg.com/?p=337#comment-577</guid>
		<description>To make it a list comprehension you can try something like this:

import operator

qset = reduce(operator.__or__, [Q(mode__exact=mode) for mode in modes[1:]])

Didn&#039;t test it, but it should work. Perhaps it can even be improved :)</description>
		<content:encoded><![CDATA[<p>To make it a list comprehension you can try something like this:</p>
<p>import operator</p>
<p>qset = reduce(operator.__or__, [Q(mode__exact=mode) for mode in modes[1:]])</p>
<p>Didn&#8217;t test it, but it should work. Perhaps it can even be improved <img src='http://www.elfsternberg.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ryan</title>
		<link>http://www.elfsternberg.com/2009/08/13/dynamic-ord-queryset-creation-with-django/comment-page-1/#comment-576</link>
		<dc:creator>Ryan</dc:creator>
		<pubDate>Fri, 14 Aug 2009 09:28:34 +0000</pubDate>
		<guid isPermaLink="false">http://www.elfsternberg.com/?p=337#comment-576</guid>
		<description>You could start off with qset = Q(), and then loop through all the modes, OR&#039;ing them together, without having to check the length.</description>
		<content:encoded><![CDATA[<p>You could start off with qset = Q(), and then loop through all the modes, OR&#8217;ing them together, without having to check the length.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Travis Cline</title>
		<link>http://www.elfsternberg.com/2009/08/13/dynamic-ord-queryset-creation-with-django/comment-page-1/#comment-575</link>
		<dc:creator>Travis Cline</dc:creator>
		<pubDate>Fri, 14 Aug 2009 04:33:30 +0000</pubDate>
		<guid isPermaLink="false">http://www.elfsternberg.com/?p=337#comment-575</guid>
		<description>A slight golfing:

qset = Q()
for mode in modes:
    qset &#124;= Q(mode=mode)
r = Responses.objects.filter(qset)

or:

r = Responses.objects.filter(reduce(lambda x,y: x&#124;Q(mode=y), modes, Q()))

But that&#039;s probably getting too clever.</description>
		<content:encoded><![CDATA[<p>A slight golfing:</p>
<p>qset = Q()<br />
for mode in modes:<br />
    qset |= Q(mode=mode)<br />
r = Responses.objects.filter(qset)</p>
<p>or:</p>
<p>r = Responses.objects.filter(reduce(lambda x,y: x|Q(mode=y), modes, Q()))</p>
<p>But that&#8217;s probably getting too clever.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

