<?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"
	>
<channel>
	<title>Comments on: django queryset weirdness</title>
	<atom:link href="http://amit.chakradeo.net/2007/04/09/django-queryset-weirdness/feed/" rel="self" type="application/rss+xml" />
	<link>http://amit.chakradeo.net/2007/04/09/django-queryset-weirdness/</link>
	<description>Stay Hungry. Stay Foolish!</description>
	<pubDate>Fri, 05 Dec 2008 12:48:30 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.5</generator>
		<item>
		<title>By: WorldMaker</title>
		<link>http://amit.chakradeo.net/2007/04/09/django-queryset-weirdness/#comment-9519</link>
		<dc:creator>WorldMaker</dc:creator>
		<pubDate>Mon, 09 Apr 2007 22:46:56 +0000</pubDate>
		<guid isPermaLink="false">http://amit.chakradeo.net/2007/04/09/django-queryset-weirdness/#comment-9519</guid>
		<description>SmileyChris is correct *everytime* you slice a queryset it runs the query.  That is: every u[0] is a different object, so u[0].save() just resaves exactly what's in the database already, because it refreshes on u[0].  Unfortunately, QuerySet just doesn't have any sort of "slice caching", so everytime you use [] you need to cache it yourself by saving it to a seperate name (in the case of a list, eg [:3], you want to pass it through list() to run the query if you aren't using a for loop).  This should probably be better documented...

(I've been bitten by this myself...  One example I've yet to fix is the random photo link on my frontpage:  due to the order_by('?') the random photo has an entirely different random alt tag and links to a further different random photo.)</description>
		<content:encoded><![CDATA[<p>SmileyChris is correct *everytime* you slice a queryset it runs the query.  That is: every u[0] is a different object, so u[0].save() just resaves exactly what&#8217;s in the database already, because it refreshes on u[0].  Unfortunately, QuerySet just doesn&#8217;t have any sort of &#8220;slice caching&#8221;, so everytime you use [] you need to cache it yourself by saving it to a seperate name (in the case of a list, eg [:3], you want to pass it through list() to run the query if you aren&#8217;t using a for loop).  This should probably be better documented&#8230;</p>
<p>(I&#8217;ve been bitten by this myself&#8230;  One example I&#8217;ve yet to fix is the random photo link on my frontpage:  due to the order_by(&#8217;?') the random photo has an entirely different random alt tag and links to a further different random photo.)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: SmileyChris</title>
		<link>http://amit.chakradeo.net/2007/04/09/django-queryset-weirdness/#comment-9509</link>
		<dc:creator>SmileyChris</dc:creator>
		<pubDate>Mon, 09 Apr 2007 22:31:33 +0000</pubDate>
		<guid isPermaLink="false">http://amit.chakradeo.net/2007/04/09/django-queryset-weirdness/#comment-9509</guid>
		<description>Saving a password isn't done inside of set_password so this is what happens:
1. &#62;&#62;&#62; u[0].save_password('testme')
2. The user's details are retrieved from the database and a User instance is created.
3. The password is changed for this instance (but it hasn't been saved).
4. &#62;&#62;&#62; u[0].save()
5. The user's details are retreived *again* from the database and a *new* User instance is created.
6. This new instance is saved (and since no data has changed for this second instance, you won't get your new password).</description>
		<content:encoded><![CDATA[<p>Saving a password isn&#8217;t done inside of set_password so this is what happens:<br />
1. &gt;&gt;&gt; u[0].save_password(&#8217;testme&#8217;)<br />
2. The user&#8217;s details are retrieved from the database and a User instance is created.<br />
3. The password is changed for this instance (but it hasn&#8217;t been saved).<br />
4. &gt;&gt;&gt; u[0].save()<br />
5. The user&#8217;s details are retreived *again* from the database and a *new* User instance is created.<br />
6. This new instance is saved (and since no data has changed for this second instance, you won&#8217;t get your new password).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: amit</title>
		<link>http://amit.chakradeo.net/2007/04/09/django-queryset-weirdness/#comment-9501</link>
		<dc:creator>amit</dc:creator>
		<pubDate>Mon, 09 Apr 2007 21:47:31 +0000</pubDate>
		<guid isPermaLink="false">http://amit.chakradeo.net/2007/04/09/django-queryset-weirdness/#comment-9501</guid>
		<description>SmileyChris: But shouldn't u[0].save() change the value in the database ? Infact, after executing the u[0].save() function, I did another User.objects.all() call and it still shows the old password.

Gábor: watch the password attribute, it needs to change after save() function, but it did not change the first time, but changed when the save method is accessed via q reference.</description>
		<content:encoded><![CDATA[<p>SmileyChris: But shouldn&#8217;t u[0].save() change the value in the database ? Infact, after executing the u[0].save() function, I did another User.objects.all() call and it still shows the old password.</p>
<p>Gábor: watch the password attribute, it needs to change after save() function, but it did not change the first time, but changed when the save method is accessed via q reference.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Gábor Farkas</title>
		<link>http://amit.chakradeo.net/2007/04/09/django-queryset-weirdness/#comment-9495</link>
		<dc:creator>Gábor Farkas</dc:creator>
		<pubDate>Mon, 09 Apr 2007 20:32:22 +0000</pubDate>
		<guid isPermaLink="false">http://amit.chakradeo.net/2007/04/09/django-queryset-weirdness/#comment-9495</guid>
		<description>hi,

maybe i overlooked something, but i can't see any problem with the example you showed. could you perhaps explain the problem in more detail?</description>
		<content:encoded><![CDATA[<p>hi,</p>
<p>maybe i overlooked something, but i can&#8217;t see any problem with the example you showed. could you perhaps explain the problem in more detail?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: SmileyChris</title>
		<link>http://amit.chakradeo.net/2007/04/09/django-queryset-weirdness/#comment-9494</link>
		<dc:creator>SmileyChris</dc:creator>
		<pubDate>Mon, 09 Apr 2007 20:07:20 +0000</pubDate>
		<guid isPermaLink="false">http://amit.chakradeo.net/2007/04/09/django-queryset-weirdness/#comment-9494</guid>
		<description>The problem here isn't queryset caching, but the fact that QuerySet objects use custom array slicing (http://www.djangoproject.com/documentation/db-api/#limiting-querysets). Every time you were doing u[0], you were getting the original object from the database again.</description>
		<content:encoded><![CDATA[<p>The problem here isn&#8217;t queryset caching, but the fact that QuerySet objects use custom array slicing (http://www.djangoproject.com/documentation/db-api/#limiting-querysets). Every time you were doing u[0], you were getting the original object from the database again.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
