mercredi 13 août 2014

Django : Comment ajouter un formulaire à délimitée formset en back-end ? -Débordement de pile


Basically, what I'd like to do is take a formset that has been bounded to request.POST, add a blank form to it, and return the new formset.


class MyView(View):
def post(self, request):
MyFormSet = formset_factory(MyForm)
posted_formset = MyFormSet(request.POST)

# Append a blank form to posted_formset here
return render(request, 'mytemplate.html', {'formset': posted_formset})

Is there any clean way to do this in Django 1.6? Right now I've got a workaround that involves creating a new formset with initial set to the data bound in posted_formset, but it's kind of clunky, and I feel like there should be an easier way to do this.




This might be a possible duplicate of this question: Django: How to add an extra form to a formset after it has been constructed?


Otherwise have a look at the extra parameter for the factory function. You get more information about formsets in the Django Documentation




So it seems like there's no clean way to do this, as after a formset is bound to a QueryDict, the extra parameter passed to formset_factory becomes irrelevant. Here's how I'm doing it in my view right now:


class MyView(View):
def post(self, request):
MyFormSet = formset_factory(MyForm)
posted_formset = MyFormSet(request.POST)

initial = []
for form in posted_formset:
data = {}
for field in form.fields:
data[field] = form[field].value()
initial.append(data)

formset = AuthorFormSet(initial=initial)
return render(request, 'mytemplate.html', {'formset': formset})

Basically, I bind a formset to request.POST, then iterate through it and grab the field values and put them into an array of dicts, initial, which I pass in when I make a new formset (see docs) that will have an extra blank form at the end because it's not bound.


This should work with any form and formset. Hope it helps someone.



Basically, what I'd like to do is take a formset that has been bounded to request.POST, add a blank form to it, and return the new formset.


class MyView(View):
def post(self, request):
MyFormSet = formset_factory(MyForm)
posted_formset = MyFormSet(request.POST)

# Append a blank form to posted_formset here
return render(request, 'mytemplate.html', {'formset': posted_formset})

Is there any clean way to do this in Django 1.6? Right now I've got a workaround that involves creating a new formset with initial set to the data bound in posted_formset, but it's kind of clunky, and I feel like there should be an easier way to do this.



This might be a possible duplicate of this question: Django: How to add an extra form to a formset after it has been constructed?


Otherwise have a look at the extra parameter for the factory function. You get more information about formsets in the Django Documentation



So it seems like there's no clean way to do this, as after a formset is bound to a QueryDict, the extra parameter passed to formset_factory becomes irrelevant. Here's how I'm doing it in my view right now:


class MyView(View):
def post(self, request):
MyFormSet = formset_factory(MyForm)
posted_formset = MyFormSet(request.POST)

initial = []
for form in posted_formset:
data = {}
for field in form.fields:
data[field] = form[field].value()
initial.append(data)

formset = AuthorFormSet(initial=initial)
return render(request, 'mytemplate.html', {'formset': formset})

Basically, I bind a formset to request.POST, then iterate through it and grab the field values and put them into an array of dicts, initial, which I pass in when I make a new formset (see docs) that will have an extra blank form at the end because it's not bound.


This should work with any form and formset. Hope it helps someone.


0 commentaires:

Enregistrer un commentaire