<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>David Janes&#039; Code Weblog &#187; django</title>
	<atom:link href="http://code.davidjanes.com/blog/category/django/feed/" rel="self" type="application/rss+xml" />
	<link>http://code.davidjanes.com/blog</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Sun, 11 Apr 2010 12:32:10 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Django 1.1 and ImageField</title>
		<link>http://code.davidjanes.com/blog/2009/11/16/django-1-1-and-imagefield/</link>
		<comments>http://code.davidjanes.com/blog/2009/11/16/django-1-1-and-imagefield/#comments</comments>
		<pubDate>Mon, 16 Nov 2009 12:42:12 +0000</pubDate>
		<dc:creator>David Janes</dc:creator>
				<category><![CDATA[code fragments]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://code.davidjanes.com/blog/?p=616</guid>
		<description><![CDATA[Having recently upgraded to Django 1.1, I suddenly started getting the error messages that look like:
  File "/Library/Python/2.6/site-packages/django/db/models/fields/related.py", line 257, in __get__
    rel_obj = QuerySet(self.field.rel.to).get(**params)
  File "/Library/Python/2.6/site-packages/django/db/models/query.py", line 300, in get
    num = len(clone)
  File "/Library/Python/2.6/site-packages/django/db/models/query.py", line 81, in __len__
    self._result_cache = list(self.iterator())
 [...]]]></description>
			<content:encoded><![CDATA[<p>Having recently upgraded to Django 1.1, I suddenly started getting the error messages that look like:</p>
<pre>  File "/Library/Python/2.6/site-packages/django/db/models/fields/related.py", line 257, in __get__
    rel_obj = QuerySet(self.field.rel.to).get(**params)
  File "/Library/Python/2.6/site-packages/django/db/models/query.py", line 300, in get
    num = len(clone)
  File "/Library/Python/2.6/site-packages/django/db/models/query.py", line 81, in __len__
    self._result_cache = list(self.iterator())
  File "/Library/Python/2.6/site-packages/django/db/models/query.py", line 251, in iterator
    obj = self.model(*row[index_start:aggregate_start])
  File "/Library/Python/2.6/site-packages/django/db/models/base.py", line 324, in __init__
    signals.post_init.send(sender=self.__class__, instance=self)
  File "/Library/Python/2.6/site-packages/django/dispatch/dispatcher.py", line 166, in send
    response = receiver(signal=self, sender=sender, **named)
  File "/Library/Python/2.6/site-packages/django/db/models/fields/files.py", line 368, in update_dimension_fields
    (self.width_field and not getattr(instance, self.width_field))
AttributeError: 'Icon' object has no attribute 'width'</pre>
<p>The issue turns out to be that you can&#8217;t just define the <code>ImageField</code> in your model, you also have to explicitly define the fields that will store the width and height fields for the image field. The sql generation tools for Django don&#8217;t do it for you.</p>
<p>For various reasons, I can&#8217;t do that this at this moment so I made the following I hack which I strongly recommend you don&#8217;t use (for efficiency reasons, as with this the height &amp; width have to be computed every time you access the image). This is added to <code>site-packages/django/db/models/fields</code> around line 367.</p>
<pre>if self.width_field and not hasattr(instance, self.width_field):
     dimension_fields_filled = False
else:
     dimension_fields_filled = not(
          (self.width_field and not getattr(instance, self.width_field))
          or (self.height_field and not getattr(instance, self.height_field))
     )</pre>
<p>The proper solutions probably involve:</p>
<ul>
<li>not adding the hack above and explicitly adding the fields, as per <a href="http://code.djangoproject.com/ticket/11196">here</a></li>
<li> updating the documentation (<a href="http://docs.djangoproject.com/en/dev/ref/models/fields/#imagefield">here</a> and <a href="http://docs.djangoproject.com/en/dev/topics/files/#using-files-in-models">here</a>) to say &#8220;you also have to add the fields to the DB&#8221;</li>
<li>making <code>syncdb</code>/<code>sql</code> automatically generate the width &amp; height fields</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://code.davidjanes.com/blog/2009/11/16/django-1-1-and-imagefield/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
