CBV for Nested Formesets

31 views
Skip to first unread message

Lee Hinde

unread,
Nov 9, 2012, 6:54:04 PM11/9/12
to django...@googlegroups.com
I'm looking at these two blog posts to help me figure out nested formsets:



The example uses a function view and I was interested using class-based views, but I can't figure out where to start.

Thanks for any pointers.

 - Lee

Kevin

unread,
Nov 11, 2012, 5:51:21 PM11/11/12
to django...@googlegroups.com
This example code and example is based on the tutorial provided in your URL.  Formsets do not have a save_all() function by default.  The provided save_all() does not return back any object, so you should provide a success_url or override the function get_success_url() in your class.
You'll need to override the "form_valid()" method to call save_all(), rather than a simple save().  Here's a simple example:

class FormsetView(UpdateView):
    model = Block
    form_class = BuildingFormset
    def form_valid(self, form):
        self.object = form.save_all()
        return super(ModelFormMixin, self).form_valid(form)

Use the other code from the blog. self.object is used if you don't specify a success_url, it uses it like self.object.get_absolute_url() to determine the URL you go to once the form is successfully saved.  This example code here should work, as the API for a Formset is very similar to a Form.

For more information on how to use class-based views, be sure to check out my latest blog post on the subject, it may provide some useful information on what functions to override:

http://www.pythondiary.com/blog/Nov.11,2012/mapping-out-djangos-class-based-views.html

Best Regards,
  Kevin Veroneau
Reply all
Reply to author
Forward
0 new messages