68👍
Key points is:
-
generated
FormSet
s withinforms.py
usinginlineformset_factory
:BookImageFormSet = inlineformset_factory(BookForm, BookImage, extra=2) BookPageFormSet = inlineformset_factory(BookForm, BookPage, extra=5)
-
returned the
FormSet
s within aCreateView
class inviews.py
:def get_context_data(self, **kwargs): context = super(BookCreateView, self).get_context_data(**kwargs) if self.request.POST: context['bookimage_form'] = BookImageFormSet(self.request.POST) context['bookpage_form'] = BookPageFormSet(self.request.POST) else: context['bookimage_form'] = BookImageFormSet() context['bookpage_form'] = BookPageFormSet() return context
-
Used
form_valid
to save the form and formset:def form_valid(self, form): context = self.get_context_data() bookimage_form = context['bookimage_formset'] bookpage_form = context['bookpage_formset'] if bookimage_form.is_valid() and bookpage_form.is_valid(): self.object = form.save() bookimage_form.instance = self.object bookimage_form.save() bookpage_form.instance = self.object bookpage_form.save() return HttpResponseRedirect('thanks/') else: return self.render_to_response(self.get_context_data(form=form))
19👍
I just added my own version after checking out some of those pre-made CBVs. I specifically needed control over multiple formsets -> one parent
in a single view each with individual save functions.
I basically stuffed the FormSet data binding into a get_named_formsets
function which is called by get_context_data
and form_valid
.
There, I check if all formsets are valid, and also look for a method that overrides a plain old formset.save()
on a per formset basis for custom saving.
The template renders formsets via
{% with named_formsets.my_specific_formset as formset %}
{{ formset }}
{{ formset.management_form }}
{% endwith %}
I think I’ll be using this system regularly.
class MyView(UpdateView): # FormView, CreateView, etc
def get_context_data(self, **kwargs):
ctx = super(MyView, self).get_context_data(**kwargs)
ctx['named_formsets'] = self.get_named_formsets()
return ctx
def get_named_formsets(self):
return {
'followup': FollowUpFormSet(self.request.POST or None, prefix='followup'),
'action': ActionFormSet(self.request.POST or None, prefix='action'),
}
def form_valid(self, form):
named_formsets = self.get_named_formsets()
if not all((x.is_valid() for x in named_formsets.values())):
return self.render_to_response(self.get_context_data(form=form))
self.object = form.save()
# for every formset, attempt to find a specific formset save function
# otherwise, just save.
for name, formset in named_formsets.items():
formset_save_func = getattr(self, 'formset_{0}_valid'.format(name), None)
if formset_save_func is not None:
formset_save_func(formset)
else:
formset.save()
return http.HttpResponseRedirect('')
def formset_followup_valid(self, formset):
"""
Hook for custom formset saving.. useful if you have multiple formsets
"""
followups = formset.save(commit=False) # self.save_formset(formset, contact)
for followup in followups:
followup.who = self.request.user
followup.contact = self.object
followup.save()
- [Django]-Django's self.client.login(…) does not work in unit tests
- [Django]-Django – filtering on foreign key properties
- [Django]-Assign variables to child template in {% include %} tag Django
12👍
You should try out django-extra-views. Look for CreateWithInlinesView
and UpdateWithInlinesView
.
- [Django]-Auto-reloading of code changes with Django development in Docker with Gunicorn
- [Django]-Django: show/log ORM sql calls from python shell
- [Django]-Django-allauth social account connect to existing account on login
2👍
I made some modification to original solution to let formset.is_valid() to work:
if self.request.POST:
context['fs'] = MyInlineFS(self.request.POST, instance=self.object)
else:
context['fs'] = MyInlineFS(instance=self.object)
- [Django]-ImproperlyConfiguredError about app_name when using namespace in include()
- [Django]-How to add an model instance to a django queryset?
- [Django]-Django Query Related Field Count
1👍
I red the generic source code of the 1.3-beta-1 :
The code is absolutely not ready for List editing or there is some black magic here.
But I think that it can be implemented quickly.
If you look at the django.view.generic.edit (that support detailed object editing) module how it use the django.view.generic.detail module.
I think that a django.view.generic.list_edit module can be implemented using django.view.generic.list and some part from django.view.generic.edit.
- [Django]-A field with precision 10, scale 2 must round to an absolute value less than 10^8
- [Django]-Django: show/log ORM sql calls from python shell
- [Django]-Adding new custom permissions in Django
1👍
The code in Jordan’s answer didn’t work for me. I posted my own question about this, which I believe I’ve now solved. The first argument to inlineformset_factory should be Book, not BookForm.
- [Django]-Authenticate by IP address in Django
- [Django]-IOS app with Django
- [Django]-In Django – Model Inheritance – Does it allow you to override a parent model's attribute?
1👍
I needed to make one more modification to Jordan’s and Speq’s view’s get_context_data()
in order to have formset.non_form_errors
exist in the template context.
...
if self.request.POST:
context['fs'] = MyInlineFS(self.request.POST, instance=self.object)
context['fs'].full_clean() # <-- new
else:
context['fs'] = MyInlineFS(instance=self.object)
return context
- [Django]-Itertools.groupby in a django template
- [Django]-Warning: Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'
- [Django]-Django 1.3.1 compilemessages. Error: sh: msgfmt: command not found