问题
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