title: Tamporarily saving and sanitizing image objects in Django
body: <p>I'm creating a Django website where users post details of used items they want to sell/barter. </p>
  
  <p>When posting an item, the second-last step is to upload (upto 3) photos of the item on sale. The last step is to provide personal details (name, address, mobile). After this, the ad is finalized and goes into a "pending approval" queue.</p>
  
  <p>I don't want to save photos to the DB <em>until an advert is finalized</em>. Hence I'm thinking I'll temporarily save a reference to the <a href="https://docs.djangoproject.com/en/1.11/ref/files/uploads/#django.core.files.uploadedfile.UploadedFile" rel="nofollow noreferrer">UploadedFile</a> object retrieved from the form like so:</p>
  
  <pre><code>request.session["photo"] = form.cleaned_data.get('photo',None)
  </code></pre>
  
  <p>So far so good, or is this problematic? Assuming everything is correct so far.</p>
  
  <p>Next, once ad is final, I'll save the images to my storage backend and <strong>pop</strong> <code>photo</code> from the <code>request.session</code> dictionary.</p>
  
  <p>But what if the user drops out before finalizing the ad? How would I handle cases where a <code>request.session["photo"]</code> entry was created, <em>but the user never finished the ad</em>? Where are UploadedFile objects saved? </p>
  
  <p>Basically, I need an efficient way to process 'orphaned' request.session image entries and related UploadedFile objects. </p>