How to get multiple files from a django FileField after model form object has been saved when form.save() does not return object

旧巷老猫 提交于 2020-01-16 08:43:12

问题


I have a django form class that extends django-postman's WriteForm (which is a ModelForm) with an additional field to upload some files.

from postman.forms import WriteForm

class MyWriteForm(WriteForm):
    file_field = forms.FileField(widget=forms.ClearableFileInput(attrs={'multiple': True}))

However, I need the saved model before I can do anything with the files. If I follow the example in the docs, I can extend postman's FormView and overwrite the save() method:

from postman.views import FormView

class MyFormView(FormView):
    form_class = MyWriteForm

    def post(self, request, *args, **kwargs):
        form_class = self.get_form_class()
        form = self.get_form(form_class)
        files = request.FILES.getlist('file_field')
        if form.is_valid():
            for f in files:
                # Do something with each file.
                result = form.save()  # result is a Boolean instead of the object!

            return self.form_valid(form)
        else:
            return self.form_invalid(form)

However, django-postman WriteForm's save() method does not return the object as expected, and instead returns a Boolean.

Is there any other way I can get the ModelForm's object after it is saved? Either through the view or the form?

来源:https://stackoverflow.com/questions/57559590/how-to-get-multiple-files-from-a-django-filefield-after-model-form-object-has-be

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!