title: Programmatically saving image to Django ImageField
body: <p>Ok, I've tried about near everything and I cannot get this to work.</p>
  
  <ul>
  <li>I have a Django model with an ImageField on it</li>
  <li>I have code that downloads an image via HTTP (tested and works)</li>
  <li>The image is saved directly into the 'upload_to' folder (the upload_to being the one that is set on the ImageField)</li>
  <li>All I need to do is associate the already existing image file path with the ImageField</li>
  </ul>
  
  <p>I've written this code about 6 different ways.</p>
  
  <p>The problem I'm running into is all of the code that I'm writing results in the following behavior:
  (1) Django will make a 2nd file, (2) rename the new file, adding an _ to the end of the file name, then (3) not transfer any of the data over leaving it basically an empty re-named file.  What's left in the 'upload_to' path is 2 files, one that is the actual image, and one that is the name of the image,but is empty, and of course the ImageField path is set to the empty file that Django try to create.</p>
  
  <p>In case that was unclear, I'll try to illustrate:</p>
  
  <pre><code>## Image generation code runs.... 
  /Upload
       generated_image.jpg     4kb
  
  ## Attempt to set the ImageField path...
  /Upload
       generated_image.jpg     4kb
       generated_image_.jpg    0kb
  
  ImageField.Path = /Upload/generated_image_.jpg
  </code></pre>
  
  <p>How can I do this without having Django try to re-store the file?  What I'd really like is something to this effect...</p>
  
  <pre><code>model.ImageField.path = generated_image_path
  </code></pre>
  
  <p>...but of course that doesn't work.</p>
  
  <p>And yes I've gone through the other questions here like <a href="https://stackoverflow.com/questions/811167/how-to-manually-assign-imagefield-in-django">this one</a> as well as the django doc on <a href="http://docs.djangoproject.com/en/dev/ref/files/file/#django.core.files.File.save" rel="noreferrer">File</a></p>
  
  <p><strong>UPDATE</strong>
  After further testing, it only does this behavior when running under Apache on Windows Server.  While running under the 'runserver' on XP it does not execute this behavior. </p>
  
  <p>I am stumped.</p>
  
  <p>Here is the code which runs successfully on XP...</p>
  
  <pre><code>f = open(thumb_path, 'r')
  model.thumbnail = File(f)
  model.save()
  </code></pre>