Monday, September 25, 2017

Re: How to clean a form which receives multiple files

There's a pull request to allow FileField(multiple=True): https://github.com/django/django/pull/9011

On Monday, September 25, 2017 at 4:19:24 AM UTC-4, guettli wrote:
thank you very much for your Feedback!

Am Freitag, 22. September 2017 18:24:23 UTC+2 schrieb mike:
I do it this way too and agree that it feels dirty. Curious to hear what others have to say about this!

On Fri, Sep 22, 2017 at 9:18 AM, guettli <guet...@gmail.com> wrote:
I am not happy with this snippet from the django docs:


from
django.views.generic.edit import FormView from .forms import FileFieldForm class FileFieldView(FormView): form_class = FileFieldForm template_name = 'upload.html' # Replace with your template. success_url = '...' # Replace with your URL or reverse(). 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. return self.form_valid(form) else: return self.form_invalid(form)

Source: https://docs.djangoproject.com/en/1.11/topics/http/file-uploads/#uploading-multiple-files

My problem: The form instance can't validate the files.

AFAIK in the method form.clean() I don't have a list of files.

I can work around this, but it breaks the nice separation of concerns which
I usually get with the nice django form library :-)

Imagine my app receives three files. Is there a way to validate these files inside form.clean()?

Regards,
Thomas Güttler

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/07e6b78b-6af2-466b-acb3-0a285c3e201b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/1dfd59e3-0bb6-4ef5-b1b6-e2dd083bd224%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment