Python + Django: Can't use "setattr" with FileField?

I recently found a really good minimalist example showing how to upload files using Python and DJango

http://stackoverflow.com/questions/5871730/need-a-minimal-django-file-upload-example

It works just great on my server. However, I need to do something just a little different that should work, but doesn’t. See subsection number 4, where he has the portion of the “views.py” file code that reads as follows:


form = DocumentForm(request.POST, request.FILES)
if form.is_valid():
	newdoc = Document(docfile = request.FILES['docfile'])
	newdoc.save()

Instead, in my use case scenario, it looks more like this:


newdoc = Document()
form = DocumentForm(request.POST, request.FILES)
if form.is_valid():
	for each in form:
		if each.name != "ID":
			if type(each.field) is forms.FileField:
				setattr(newdoc, each.name, request.FILES[each.name])

			else
				setattr(newdoc, each.name, form.cleaned_data[each.name])
	newdoc.save()

Since “setattr” effectively does the same thing as his original example does, and since the model field and form field names are identically named “docfile”, I’m effectively trying to do this:


setattr(newdoc, 'docfile', request.FILES['docfile'])

You can copy and paste my code right into his example to test. But unfortunately its not working. Can anyone help point me in the right direction as to what I might be doing wrong here?

UPDATE:

I just figured it out. Its my own fault. It DOES work, but I goofed on the names in my project. They’re NOT identical, and I missed it. I was basically doing the same as previously mentioned, except instead my “models.py” class was as such:


class UploadedPictures(models.Model):

	Title = models.CharField(max_length = 100)
	Picture = models.FileField(upload_to = 'documents/%Y/%m/%d')
	PictureThumb = models.FileField(upload_to = 'documents/%Y/%m/%d')
	def __unicode__(self):
		return self.Title



…and my “forms.py” was as such:


class UploadedPicturesForm(forms.Form):
	
	ID = forms.CharField(widget = forms.HiddenInput())
	Title = forms.CharField()	
	PictureURL = forms.FileField()
	PictureThumbURL = forms.FileField()


The differences were “Picture” versus “PictureURL” and “PictureThumb” versus “PictureThumbURL” and my blind eye was just missing it for some reason. So when I was doing my “for each” loop, the setattr command was using the wrong name and the info never got saved and the file never got uploaded. I’m such a doofus.