Django formset : Save multiple forms from formset
up vote
0
down vote
favorite
I spent several days in order to add dynamically forms to formset
and I find a way to do that. But I have a little issue with forms saving
because it just saves the first form and not all forms from formset.
This is my formset :
DocumentFormSets = inlineformset_factory(Publication, Document, form=DocumentForm, extra=1, max_num=4)
This is my template :
<fieldset>
<legend class="title"><span class="name">{% trans 'Document form' %}</span></legend>
{{ DocumentFormSet.management_form }}
<div id="form_set">
{% for form in DocumentFormSet.forms %}
<div class='formset-document'>
<table class='no_error'>
<div class="row">
<div class="col-xs-3">
{{ form.title|as_crispy_field }}
</div>
<div class="col-xs-3">
{{ form.language|as_crispy_field }}
</div>
<div class="col-xs-3">
{{ form.format|as_crispy_field }}
</div>
<div class="col-xs-3">
{{ form.upload|as_crispy_field }}
</div>
</div>
</table>
</div>
{% endfor %}
</div>
<br>
<input type="button" class="btn btn-default" value="Add More" id="add_more">
<div id="empty_form" style="display:none">
<table class='no_error'>
<div class="row">
<div class="col-xs-3">
{{ DocumentFormSet.empty_form.title|as_crispy_field }}
</div>
<div class="col-xs-3">
{{ DocumentFormSet.empty_form.language|as_crispy_field }}
</div>
<div class="col-xs-3">
{{ DocumentFormSet.empty_form.format|as_crispy_field }}
</div>
<div class="col-xs-3">
{{ DocumentFormSet.empty_form.upload|as_crispy_field }}
</div>
</div>
</table>
</div>
</fieldset>
This is my JS part :
$('#add_more').click(function () {
var form_idx = $('#id_form-TOTAL_FORMS').val();
$('#form_set').append($('#empty_form').html().replace(/__prefix__/g, form_idx));
$('#id_form-TOTAL_FORMS').val(parseInt(form_idx) + 1);
});
And this is my views.py file :
class PublicationCreateView(EdqmCreateView):
""" Create publication with document form through formsets """
model = Publication
template_name = 'publication/publication_form.html'
def get_context_data(self, **kwargs):
context = super(PublicationCreateView, self).get_context_data(**kwargs)
context['DocumentFormSets'] = DocumentFormSets(self.request.POST or None, self.request.FILES or None, prefix='doc')
return context
def form_valid(self, form):
context = self.get_context_data()
formsets = context['DocumentFormSets']
if form.is_valid() and formsets.is_valid():
self.object = form.save()
for document in formsets:
self.object = document.save()
document.instance = self.object
document.save()
return super(PublicationCreateView, self).form_valid(form)
But I get this issue :
save() prohibited to prevent data loss due to unsaved related object
'publication'.
How I can save my forms from formsets ?
Thank you !
django django-forms formset
|
show 3 more comments
up vote
0
down vote
favorite
I spent several days in order to add dynamically forms to formset
and I find a way to do that. But I have a little issue with forms saving
because it just saves the first form and not all forms from formset.
This is my formset :
DocumentFormSets = inlineformset_factory(Publication, Document, form=DocumentForm, extra=1, max_num=4)
This is my template :
<fieldset>
<legend class="title"><span class="name">{% trans 'Document form' %}</span></legend>
{{ DocumentFormSet.management_form }}
<div id="form_set">
{% for form in DocumentFormSet.forms %}
<div class='formset-document'>
<table class='no_error'>
<div class="row">
<div class="col-xs-3">
{{ form.title|as_crispy_field }}
</div>
<div class="col-xs-3">
{{ form.language|as_crispy_field }}
</div>
<div class="col-xs-3">
{{ form.format|as_crispy_field }}
</div>
<div class="col-xs-3">
{{ form.upload|as_crispy_field }}
</div>
</div>
</table>
</div>
{% endfor %}
</div>
<br>
<input type="button" class="btn btn-default" value="Add More" id="add_more">
<div id="empty_form" style="display:none">
<table class='no_error'>
<div class="row">
<div class="col-xs-3">
{{ DocumentFormSet.empty_form.title|as_crispy_field }}
</div>
<div class="col-xs-3">
{{ DocumentFormSet.empty_form.language|as_crispy_field }}
</div>
<div class="col-xs-3">
{{ DocumentFormSet.empty_form.format|as_crispy_field }}
</div>
<div class="col-xs-3">
{{ DocumentFormSet.empty_form.upload|as_crispy_field }}
</div>
</div>
</table>
</div>
</fieldset>
This is my JS part :
$('#add_more').click(function () {
var form_idx = $('#id_form-TOTAL_FORMS').val();
$('#form_set').append($('#empty_form').html().replace(/__prefix__/g, form_idx));
$('#id_form-TOTAL_FORMS').val(parseInt(form_idx) + 1);
});
And this is my views.py file :
class PublicationCreateView(EdqmCreateView):
""" Create publication with document form through formsets """
model = Publication
template_name = 'publication/publication_form.html'
def get_context_data(self, **kwargs):
context = super(PublicationCreateView, self).get_context_data(**kwargs)
context['DocumentFormSets'] = DocumentFormSets(self.request.POST or None, self.request.FILES or None, prefix='doc')
return context
def form_valid(self, form):
context = self.get_context_data()
formsets = context['DocumentFormSets']
if form.is_valid() and formsets.is_valid():
self.object = form.save()
for document in formsets:
self.object = document.save()
document.instance = self.object
document.save()
return super(PublicationCreateView, self).form_valid(form)
But I get this issue :
save() prohibited to prevent data loss due to unsaved related object
'publication'.
How I can save my forms from formsets ?
Thank you !
django django-forms formset
to make things easier to read, you should use a plural for your formset. But why are you assigning something todocument
and savingdocument
inside the for loop ifdocument
is your formset?
– dirkgroten
16 hours ago
Also on which line exactly do you get the error? And show us how you construct the formset (your get_context_data)
– dirkgroten
16 hours ago
@dirkgroten It's the first time I use formsets, so some things are a bit unclear yet. I addedget_context_data
in my question. The issue is on line :self.object = form.save()
– Ducky
16 hours ago
isDocumentFormset
created using aninlineformset_factory
?
– dirkgroten
16 hours ago
Yes exactly ! I edited my code in order to show you missing elements.
– Ducky
16 hours ago
|
show 3 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I spent several days in order to add dynamically forms to formset
and I find a way to do that. But I have a little issue with forms saving
because it just saves the first form and not all forms from formset.
This is my formset :
DocumentFormSets = inlineformset_factory(Publication, Document, form=DocumentForm, extra=1, max_num=4)
This is my template :
<fieldset>
<legend class="title"><span class="name">{% trans 'Document form' %}</span></legend>
{{ DocumentFormSet.management_form }}
<div id="form_set">
{% for form in DocumentFormSet.forms %}
<div class='formset-document'>
<table class='no_error'>
<div class="row">
<div class="col-xs-3">
{{ form.title|as_crispy_field }}
</div>
<div class="col-xs-3">
{{ form.language|as_crispy_field }}
</div>
<div class="col-xs-3">
{{ form.format|as_crispy_field }}
</div>
<div class="col-xs-3">
{{ form.upload|as_crispy_field }}
</div>
</div>
</table>
</div>
{% endfor %}
</div>
<br>
<input type="button" class="btn btn-default" value="Add More" id="add_more">
<div id="empty_form" style="display:none">
<table class='no_error'>
<div class="row">
<div class="col-xs-3">
{{ DocumentFormSet.empty_form.title|as_crispy_field }}
</div>
<div class="col-xs-3">
{{ DocumentFormSet.empty_form.language|as_crispy_field }}
</div>
<div class="col-xs-3">
{{ DocumentFormSet.empty_form.format|as_crispy_field }}
</div>
<div class="col-xs-3">
{{ DocumentFormSet.empty_form.upload|as_crispy_field }}
</div>
</div>
</table>
</div>
</fieldset>
This is my JS part :
$('#add_more').click(function () {
var form_idx = $('#id_form-TOTAL_FORMS').val();
$('#form_set').append($('#empty_form').html().replace(/__prefix__/g, form_idx));
$('#id_form-TOTAL_FORMS').val(parseInt(form_idx) + 1);
});
And this is my views.py file :
class PublicationCreateView(EdqmCreateView):
""" Create publication with document form through formsets """
model = Publication
template_name = 'publication/publication_form.html'
def get_context_data(self, **kwargs):
context = super(PublicationCreateView, self).get_context_data(**kwargs)
context['DocumentFormSets'] = DocumentFormSets(self.request.POST or None, self.request.FILES or None, prefix='doc')
return context
def form_valid(self, form):
context = self.get_context_data()
formsets = context['DocumentFormSets']
if form.is_valid() and formsets.is_valid():
self.object = form.save()
for document in formsets:
self.object = document.save()
document.instance = self.object
document.save()
return super(PublicationCreateView, self).form_valid(form)
But I get this issue :
save() prohibited to prevent data loss due to unsaved related object
'publication'.
How I can save my forms from formsets ?
Thank you !
django django-forms formset
I spent several days in order to add dynamically forms to formset
and I find a way to do that. But I have a little issue with forms saving
because it just saves the first form and not all forms from formset.
This is my formset :
DocumentFormSets = inlineformset_factory(Publication, Document, form=DocumentForm, extra=1, max_num=4)
This is my template :
<fieldset>
<legend class="title"><span class="name">{% trans 'Document form' %}</span></legend>
{{ DocumentFormSet.management_form }}
<div id="form_set">
{% for form in DocumentFormSet.forms %}
<div class='formset-document'>
<table class='no_error'>
<div class="row">
<div class="col-xs-3">
{{ form.title|as_crispy_field }}
</div>
<div class="col-xs-3">
{{ form.language|as_crispy_field }}
</div>
<div class="col-xs-3">
{{ form.format|as_crispy_field }}
</div>
<div class="col-xs-3">
{{ form.upload|as_crispy_field }}
</div>
</div>
</table>
</div>
{% endfor %}
</div>
<br>
<input type="button" class="btn btn-default" value="Add More" id="add_more">
<div id="empty_form" style="display:none">
<table class='no_error'>
<div class="row">
<div class="col-xs-3">
{{ DocumentFormSet.empty_form.title|as_crispy_field }}
</div>
<div class="col-xs-3">
{{ DocumentFormSet.empty_form.language|as_crispy_field }}
</div>
<div class="col-xs-3">
{{ DocumentFormSet.empty_form.format|as_crispy_field }}
</div>
<div class="col-xs-3">
{{ DocumentFormSet.empty_form.upload|as_crispy_field }}
</div>
</div>
</table>
</div>
</fieldset>
This is my JS part :
$('#add_more').click(function () {
var form_idx = $('#id_form-TOTAL_FORMS').val();
$('#form_set').append($('#empty_form').html().replace(/__prefix__/g, form_idx));
$('#id_form-TOTAL_FORMS').val(parseInt(form_idx) + 1);
});
And this is my views.py file :
class PublicationCreateView(EdqmCreateView):
""" Create publication with document form through formsets """
model = Publication
template_name = 'publication/publication_form.html'
def get_context_data(self, **kwargs):
context = super(PublicationCreateView, self).get_context_data(**kwargs)
context['DocumentFormSets'] = DocumentFormSets(self.request.POST or None, self.request.FILES or None, prefix='doc')
return context
def form_valid(self, form):
context = self.get_context_data()
formsets = context['DocumentFormSets']
if form.is_valid() and formsets.is_valid():
self.object = form.save()
for document in formsets:
self.object = document.save()
document.instance = self.object
document.save()
return super(PublicationCreateView, self).form_valid(form)
But I get this issue :
save() prohibited to prevent data loss due to unsaved related object
'publication'.
How I can save my forms from formsets ?
Thank you !
django django-forms formset
django django-forms formset
edited 15 hours ago
asked 16 hours ago
Ducky
2,48511552
2,48511552
to make things easier to read, you should use a plural for your formset. But why are you assigning something todocument
and savingdocument
inside the for loop ifdocument
is your formset?
– dirkgroten
16 hours ago
Also on which line exactly do you get the error? And show us how you construct the formset (your get_context_data)
– dirkgroten
16 hours ago
@dirkgroten It's the first time I use formsets, so some things are a bit unclear yet. I addedget_context_data
in my question. The issue is on line :self.object = form.save()
– Ducky
16 hours ago
isDocumentFormset
created using aninlineformset_factory
?
– dirkgroten
16 hours ago
Yes exactly ! I edited my code in order to show you missing elements.
– Ducky
16 hours ago
|
show 3 more comments
to make things easier to read, you should use a plural for your formset. But why are you assigning something todocument
and savingdocument
inside the for loop ifdocument
is your formset?
– dirkgroten
16 hours ago
Also on which line exactly do you get the error? And show us how you construct the formset (your get_context_data)
– dirkgroten
16 hours ago
@dirkgroten It's the first time I use formsets, so some things are a bit unclear yet. I addedget_context_data
in my question. The issue is on line :self.object = form.save()
– Ducky
16 hours ago
isDocumentFormset
created using aninlineformset_factory
?
– dirkgroten
16 hours ago
Yes exactly ! I edited my code in order to show you missing elements.
– Ducky
16 hours ago
to make things easier to read, you should use a plural for your formset. But why are you assigning something to
document
and saving document
inside the for loop if document
is your formset?– dirkgroten
16 hours ago
to make things easier to read, you should use a plural for your formset. But why are you assigning something to
document
and saving document
inside the for loop if document
is your formset?– dirkgroten
16 hours ago
Also on which line exactly do you get the error? And show us how you construct the formset (your get_context_data)
– dirkgroten
16 hours ago
Also on which line exactly do you get the error? And show us how you construct the formset (your get_context_data)
– dirkgroten
16 hours ago
@dirkgroten It's the first time I use formsets, so some things are a bit unclear yet. I added
get_context_data
in my question. The issue is on line : self.object = form.save()
– Ducky
16 hours ago
@dirkgroten It's the first time I use formsets, so some things are a bit unclear yet. I added
get_context_data
in my question. The issue is on line : self.object = form.save()
– Ducky
16 hours ago
is
DocumentFormset
created using an inlineformset_factory
?– dirkgroten
16 hours ago
is
DocumentFormset
created using an inlineformset_factory
?– dirkgroten
16 hours ago
Yes exactly ! I edited my code in order to show you missing elements.
– Ducky
16 hours ago
Yes exactly ! I edited my code in order to show you missing elements.
– Ducky
16 hours ago
|
show 3 more comments
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53265969%2fdjango-formset-save-multiple-forms-from-formset%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
to make things easier to read, you should use a plural for your formset. But why are you assigning something to
document
and savingdocument
inside the for loop ifdocument
is your formset?– dirkgroten
16 hours ago
Also on which line exactly do you get the error? And show us how you construct the formset (your get_context_data)
– dirkgroten
16 hours ago
@dirkgroten It's the first time I use formsets, so some things are a bit unclear yet. I added
get_context_data
in my question. The issue is on line :self.object = form.save()
– Ducky
16 hours ago
is
DocumentFormset
created using aninlineformset_factory
?– dirkgroten
16 hours ago
Yes exactly ! I edited my code in order to show you missing elements.
– Ducky
16 hours ago