60

I have the following models:

class Bill(models.Model):
    date = models.DateTimeField(_("Date of bill"),null=True,blank=True)

class Item(models.Model):
    name = models.CharField(_("Name"),max_length=100)
    price = models.FloatField(_("Price"))
    quantity = models.IntegerField(_("Quantity"))
    bill = models.ForeignKey("Bill",verbose_name=_("Bill"),
                             related_name="billitem")

I know that this is possible:

from django.forms.models import inlineformset_factory
inlineformset_factory(Bill, Item)

and then process this via standard view.

Now I was wondering, if there is a way to achieve the same (meaning: using a inline for adding/editing items belonging to a bill) using class based views (not for the admin-interface).

Hixi
  • 765
  • 1
  • 8
  • 9

7 Answers7

68

Key points is:

  1. generated FormSets within forms.py using inlineformset_factory:

    BookImageFormSet = inlineformset_factory(BookForm, BookImage, extra=2)
    BookPageFormSet = inlineformset_factory(BookForm, BookPage, extra=5)
    
  2. returned the FormSets within a CreateView class in views.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
    
  3. 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))
    
Sergey Lyapustin
  • 1,917
  • 17
  • 27
Jordan Reiter
  • 20,467
  • 11
  • 95
  • 161
  • I haven't tested the code to see if it actually works, but the source code all looks good. – Jordan Reiter Jul 25 '11 at 18:20
  • 1
    I can confirm that the code above works fine, but I can't seem to figure out how to use a class-based *update* view (so UpdateView with inline formsets); the existing model data simply never enters the formset. – praseodym Jul 29 '11 at 00:28
  • You could *probably* change `BookImageFormSet()` to `BookImageFormSet(instance=self.instance)` although again I haven't tested... – Jordan Reiter Aug 02 '11 at 00:56
  • 3
    I think it'd be `self.object`, as opposed to `self.instance` – orokusaki Oct 12 '11 at 04:19
  • @praseodym This code didn't work for me. Did you change the first argument to inlineformset_factory? – knite Jun 25 '12 at 23:56
  • 5
    *form_valid* seems like the wrong place for this, as it is usually called after the form has been validated. I think overriding *post* would be the cleaner solution. – cvk Sep 04 '12 at 09:11
  • 3
    A little late to the party, but this has worked for me and @orokusaki is right about `self.object` instead of `self.instance`. – edhedges Nov 16 '12 at 05:24
  • 4
    While I think the solution is sound, it doesn't make much sense to be checking the request method in `get_context_data()`. Based on the `ProcessFormView` in the Django source, the `get()` and `post()` methods are where you should be instantiating your forms and adding them to the `get_context_data(form=form)` call. – Soviut Mar 08 '13 at 13:53
  • 5
    The example leads to the formsets being recreated when the form is invalid (see `self.get_context_data(form=form)` - this isn't a problem until you have non-form errors in your formset, which appear when you call `formset.is_valid()` (which accesses `formset.errors`, causing an error list to be instantiated). The solution it to reuse the formset (pass it to `get_context_data` and in `get_context_data` check to see if it was provided, only adding it when it's not... or use `post` like the previous comment suggests, which will lead to reusing the formset naturally). – orokusaki Mar 24 '13 at 17:58
  • 3
    There seems to be a better treatment of this here: http://kevindias.com/writing/django-class-based-views-multiple-inline-formsets/ but it seems a lot of work to do for just creating which is then replicated for edit, and if you have multiple models with the same setup this starts to become a lot of maintenance (which is what CBV are supposed to help with, aren't they?). Has this been addressed in Django 1.7? – CpILL Jan 07 '15 at 02:45
  • @CpILL That link you referenced does not work. Rather post code. – tread Oct 23 '17 at 09:31
  • @surfer190 the code I included in the rest of the answer was the code in the remote link. – Jordan Reiter Nov 07 '17 at 16:40
  • @JordanReiter is it possible to pass initial value for a list in model formset ? i have question regarding the same , https://stackoverflow.com/questions/56325133/attendance-system-using-django – Joel Deleep May 28 '19 at 14:28
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()
djvg
  • 11,722
  • 5
  • 72
  • 103
Yuji 'Tomita' Tomita
  • 115,817
  • 29
  • 282
  • 245
12

You should try out django-extra-views. Look for CreateWithInlinesView and UpdateWithInlinesView.

Udi
  • 29,222
  • 9
  • 96
  • 129
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)
Speq
  • 122
  • 1
  • 3
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.

Community
  • 1
  • 1
knite
  • 6,033
  • 6
  • 38
  • 54
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
pztrick
  • 3,741
  • 30
  • 35
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.

VGE
  • 4,171
  • 18
  • 17
  • Yes, I read that part. Though it is not an answer for "automatic" in-lines, as it states there: "A mixin that can be used to display a list of objects." I would like to add/edit/delete inlines, not diplay them ;) – Hixi Jan 25 '11 at 07:05
  • Hixi was wondering if a feature was available with DJANGO -1.3- and using class based view. I red the django source code and the feature is clearly NOT IMPLEMENTED (in django 1.3 beta 1). I give some hint to help to implement not the full developpment. – VGE May 23 '11 at 15:52