Problm using FileField with inline formsets (inlineformset_factory)

427 views
Skip to first unread message

c.po...@bangor.ac.uk

unread,
Sep 10, 2008, 4:50:29 AM9/10/08
to Django users
Hi

I'm having a problem with inline formsets containing file field, i've
had a good google but couldn't find an answer, so apolgies if this is
a stupid or repetative question.

I have a model Applicatant, which has a one to many relation with
Education which contains a filefeild. My problem is that when django
comes to validate the inline form education it reports that the field
is empty, if i tell the model to allow empty feilds then the file is
not saved

Is it possible to use file fields with the inlineformset_factory or do
i need to go at this another way?

I have included my code, pretty simple and pretty much copied from the
documentation:

def myview(request):
InlineFormSet = inlineformset_factory(Applicant, Education)
applicant = Applicant.objects.get(pk=1)
if request.method == 'POST':
formset = InlineFormSet(request.POST, request.FILES,
instance=applicant)
if formset.is_valid():
Do stuff
else:
formset = InlineFormSet(instance=applicant)

return render_to_response("mytemplate.html", { "formset":
formset,})

where mytemplate.html is simply

<form action="./do" method="POST">

{{ formset.management_form }}
<table>
{% for form in formset.forms %}
{{ form.as_ul }}
{% endfor %}
</table>

<input type="submit" name="next" value="Next">
</form>


Many than ks
Charlotte


Daniel Roseman

unread,
Sep 10, 2008, 5:46:20 AM9/10/08
to Django users
On Sep 10, 9:50 am, "c.poll...@bangor.ac.uk" <c.poll...@bangor.ac.uk>
wrote:
The problem isn't with Formsets, it's just that you haven't set your
enctype in the HTML form element. It should be:
<form enctype="multipart/form-data" action="./do" method="POST">

See http://docs.djangoproject.com/en/dev/ref/forms/api/#binding-uploaded-files-to-a-form
--
DR.

c.po...@bangor.ac.uk

unread,
Sep 10, 2008, 5:47:52 AM9/10/08
to Django users
Hoorah, i ike it when the answer is me being stupid. Much easier to
fix

Thank you

On Sep 10, 10:46 am, Daniel Roseman <roseman.dan...@googlemail.com>
wrote:
> Seehttp://docs.djangoproject.com/en/dev/ref/forms/api/#binding-uploaded-...
> --
> DR.

YusufSalahAdDin

unread,
Oct 5, 2017, 5:30:29 PM10/5/17
to Django users
You saved my ass, thanks!!!
Reply all
Reply to author
Forward
0 new messages