0

Trying to save a fileField in a formset, but the debug returns UploadPdfFormSet' object has no attribute 'save'. Feel like the documentation around handling files in formsets are lacking. Anything you guys could help me with?

forms.py

    from django import forms

class UploadPdf(forms.Form):
    docfiles = forms.FileField(label='Browse')

class UploadPdfUrl(forms.Form):
    docurl = forms.URLField(label='Address')

views.py

    def upload(request):    
        UploadPdfFormSet = formset_factory(UploadPdf)
        if request.method == 'POST':
            formsetFile = UploadPdfFormSet(request.POST, request.FILES)     
            if formsetFile.is_valid():
                instances = formsetFile.save
                for form in instances:
                    form.save()

                return HttpResponse('yey')
        else:
            formsetFile = UploadPdfFormSet()
            formURL = UploadPdfUrl

        return render(request, 'pdfchecker/index.html', {
            'formsetFile': formsetFile, 'formURL': formURL,
            })



UPDATE: New view.py file which successfully saves file:

def upload(request):

    UploadPdfFormSet = formset_factory(UploadPdf)
    handleUploadPdfFormSet = modelformset_factory(handle_UploadPdf)
    if request.method == 'POST':
        formsetFile = UploadPdfFormSet(request.POST, request.FILES)
        formsetHandle = handleUploadPdfFormSet(request.POST, request.FILES)
        if formsetFile.is_valid():
            formsetHandle.save()

            return HttpResponse('yey')
    else:
        formsetFile = UploadPdfFormSet()
        formURL = UploadPdfUrl

    return render(request, 'pdfchecker/index.html', {
        'formsetFile': formsetFile, 'formURL': formURL,
        })
Koronag
  • 157
  • 1
  • 15

1 Answers1

0

Looks like you are referring to this example of the documentation. But this example is all about ModelFormsets, not the regular formset you are using. The save method doesn't make sense here. So you have two options:

  • use ModelFormsets (of course if there are models in your app that correspond to UploadPdf form)
  • (which is more likely) do the stuff with the formset cleaned_data manually. Like in this example.
isobolev
  • 1,473
  • 20
  • 19
  • Seems like i didn't really understand forms properly. I get that you have to use the models to save the files now. Let's say i have this in my models.py: from django.db import models class handle_UploadPdf(models.Model): docfile = models.FileField(upload_to='.') Do you know how do i proceed with formsets? I've made it with normal forms, but when i introduce formsets everything just gets a bit harder i guess. – Koronag Apr 29 '14 at 09:54
  • @Koronag But what is a purpose of having this model (handle_UploadPdf)? – isobolev Apr 29 '14 at 09:58
  • I want to save the file to a specific location. As you can see now it is just marked as a ".", but i really want a specific URL as the destination. I followed this guide: http://stackoverflow.com/questions/5871730/need-a-minimal-django-file-upload-example But that one only accounts for regular forms, and not formsets. So i'm kinda confused. – Koronag Apr 29 '14 at 10:01
  • @Koronag But how do you use the data saved into this model? Is the model somehow connected to any of your other models? – isobolev Apr 29 '14 at 10:05
  • Not really. Thing is, in the example you just upload the files directly to the media storage and then generate date, time etc. What i want to do is upload the file to an external url. But it seems like this is rather decided in settings py. I've updated the view with a solution that now atleast saves the file. views.py – Koronag Apr 29 '14 at 10:12
  • My point is that there is no need to create model just to handle uploading of arbitrary files. Instead you could handle them manually like described here: https://docs.djangoproject.com/en/1.6/topics/http/file-uploads/ – isobolev Apr 29 '14 at 10:31
  • Instead of creating a model i just create a function which handles it? To be honest im a bit confused. – Koronag Apr 29 '14 at 11:10
  • @Koronag If you don't really need the data about uploaded files in the DB, than you don't need a model. In this case it's better to handle uploaded files manually, not using a fictive model. – isobolev Apr 29 '14 at 11:35
  • I see. Then i have no idea how to save the file properly. Dont really get the examples in the django ducmentation tbh. – Koronag Apr 29 '14 at 13:10