Django 1.2 modelformset_factory fails with meta: widgets

173 views
Skip to first unread message

Jason

unread,
Jul 28, 2010, 2:08:54 PM7/28/10
to Django users
For example:

class ArticleForm(ModelForm):
class Meta:
model = Article
widgets = {
'pub_date': SplitSelectDateTimeWidget(),
'expire_date': CalendarWidget(attrs={'class':'date-
pick'})
}


And in a view function:
...
ArticleFormSet = modelformset_factory(Article,
form = ArticleForm,
extra=0)
...

Removing 'widgets' from the Meta in ArticleForm fixes the error.

The new widgets convention here is really handy. I don't want to lose
it!

Any tips?

Daniel Roseman

unread,
Jul 28, 2010, 3:00:03 PM7/28/10
to Django users
How does it fail? What error do you get? If there's a traceback,
please post it here.
--
DR.

Jason

unread,
Jul 28, 2010, 3:50:35 PM7/28/10
to Django users
Traceback:
File "C:\Python25\lib\site-packages\django\core\handlers\base.py" in
get_response
100. response = callback(request,
*callback_args, **callback_kwargs)
File "C:\Documents and Settings\goodrich\PycharmProjects\CCC\Aggregator
\newsmail\views.py" in manage_articles
174. form = ArticleForm)
File "C:\Python25\lib\site-packages\django\forms\models.py" in
modelformset_factory
669.
formfield_callback=formfield_callback)
File "C:\Python25\lib\site-packages\django\forms\models.py" in
modelform_factory
407. return ModelFormMetaclass(class_name, (form,),
form_class_attrs)
File "C:\Python25\lib\site-packages\django\forms\models.py" in __new__
220. opts.exclude,
opts.widgets, formfield_callback)
File "C:\Python25\lib\site-packages\django\forms\models.py" in
fields_for_model
178. formfield = formfield_callback(f, **kwargs)

Exception Type: TypeError at /newsmail/manage/
Exception Value: <lambda>() got an unexpected keyword argument
'widget'

Jason

unread,
Jul 29, 2010, 12:33:08 PM7/29/10
to Django users
Can anyone confirm that passing in a form with Meta.widgets set to
modelformset_factory() does in fact work?

I've tried stripping my code down to the basics and still get the same
exception. Debugging Django code doesn't help me because it fails
during a lamda function that I don't quite understand.

If anyone else has this problem I'll go ahead and submit a bug report.

Matthew R

unread,
Aug 13, 2010, 11:55:22 AM8/13/10
to Django users
I got hit by this same bug and here's the workaround I used, for
future reference. Basically you need to specify a formfield_callback
kwarg to modelformset_factory that just passes along any kwargs it
receives (namely, in this case, the 'widget' argument) to
Field.formfield():

def create_formfield(f, **kwargs):
return f.formfield(**kwargs)

ArticleFormSet = modelformset_factory(Article,
form = ArticleForm,
formfield_callback=create_formfield,
extra=0)

Jason

unread,
Sep 1, 2010, 12:01:37 PM9/1/10
to Django users
Thanks! I'll give that a try.

On Aug 13, 8:55 am, Matthew R <mrich...@gmail.com> wrote:
> I got hit by this same bug and here's the workaround I used, for
> future reference. Basically you need to specify a formfield_callback
> kwarg to modelformset_factory that just passes along any kwargs it
> receives (namely, in this case, the 'widget' argument) to
> Field.formfield():
>
> def create_formfield(f, **kwargs):
>     return f.formfield(**kwargs)
>
> ArticleFormSet = modelformset_factory(Article,
>   form = ArticleForm,
>   formfield_callback=create_formfield,
>   extra=0)
>
> On Jul 29, 11:33 am, Jason <goodri...@gmail.com> wrote:
>
> > Can anyone confirm that passing in a form withMeta.widgets set to
> > > > > Removing 'widgets' from theMetain ArticleForm fixes the error.

refreegrata

unread,
Sep 1, 2010, 1:59:17 PM9/1/10
to Django users
rewrite the __init__., redefining the form element in this place. The
error must to dissapear.

class FormMyForm(ModelForm):
def __init__(self, *args, **kwargs):
super(FormMyForm, self).__init__(*args, **kwargs)
self.fields['aaaa'] =
forms.CharField(widget=forms.TextInput(attrs={.....}))

class Meta:
model = Model
fields = ['aaaa']
Reply all
Reply to author
Forward
0 new messages