formset in form_class

18 views
Skip to first unread message

scha...@gmail.com

unread,
Jan 31, 2017, 11:55:15 AM1/31/17
to Django users
Hi All,
in a view deriving from UpdateView the form_class attribute gets assigned with a formset.
the View then overrides the get_form function.
class RLFormView(LoginRequiredMixin, StaffuserRequiredMixin, UpdateView):
    template_name
= 'abc/layout.html'
    form_class
= LayoutFormSet
    fields
= '__all__'
....
   
def get_form(self, form_class=None):
       
if self.request.POST:
           
return form_class(self.request.POST)
       
else:
            initial
= [{'param': 'a',
                       
'choosen': 'value'}]
           
return form_class(initial=initial)


I'm just wondering if this is a nice way of using the formset? Normally a form is assigned to form_class.

Thanks for hints
Schaf

scha...@gmail.com

unread,
Feb 1, 2017, 12:19:08 PM2/1/17
to Django users
Hi All,
problem solved.
The problem was the default value None for the form_class parameter at get_form.
It works when changing:
def get_form(self, form_class=None):
to:
def get_form(self, form_class=LayoutFormSet):

Cheers
Schaf

Melvyn Sopacua

unread,
Feb 3, 2017, 11:00:37 AM2/3/17
to django...@googlegroups.com

On Tuesday 31 January 2017 08:55:15 scha...@gmail.com wrote:

 

> class RLFormView(LoginRequiredMixin, StaffuserRequiredMixin,

> UpdateView): template_name = 'abc/layout.html'

> form_class = LayoutFormSet

> fields = '__all__'

> ....

 

The easy fix (and proper way to support the declarative pattern):

> def get_form(self, form_class=None):

form_class = form_class or self.form_class

> if self.request.POST:

> return form_class(self.request.POST)

> else:

> initial = [{'param': 'a',

> 'choosen': 'value'}]

> return form_class(initial=initial)

 

--

Melvyn Sopacua

Reply all
Reply to author
Forward
0 new messages