SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Sep 2013
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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/5...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:

    Code Python:
    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:

    Code Python:
    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:

    Code Python:
    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?

  2. #2
    SitePoint Member
    Join Date
    Sep 2013
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy FIXED. I figured this out. Names were incorrect and not matching.

    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:

    Code Python:
    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:

    Code Python:
    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.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •