Ben
Ben

Reputation: 16610

Django file upload...why is this working?

This is the first time I've tried to upload a file with Django. I did something and it worked, though I realized later that it's not the correct way to do it. When I called save on the object, did it call a built-in handler for the FileField? I realize that I should create my own handler but I was just curious why this worked.

def upload_test(request):
user=User.objects.get(pk=user.id)
photoform=PhotoForm()
if request.method=='POST':
    photoform=Post_PhotoForm(request.POST,request.FILES)
    if photoform.is_valid():
        photo=photoform.save(commit=False)
        photo.user=user
        photo.save()
        return HttpResponse('success')
    else:
        return HttpResponse('%s' %photoform.errors)
return render_to_response("site/upload_test.html", {'photoform':photoform}, context_instance=RequestContext(request))   

This is saving the object and uploading the file to the directory specified in the FileField.

If I create a handler which writes the file in chunks, how can I also save the photoform instance? Will it create duplicates?

Thanks for the insight.

Upvotes: 2

Views: 314

Answers (1)

DrMeers
DrMeers

Reputation: 4207

I imagine PhotoForm is a ModelForm? Manual handling of uploaded files as described in the docs is only required for standard Forms. The chunk handling is performed in the background by the models.FileField and its storage object, etc.

Upvotes: 1

Related Questions