diff --git a/formsetfield/fields.py b/formsetfield/fields.py index 44466b9..055f694 100644 --- a/formsetfield/fields.py +++ b/formsetfield/fields.py @@ -6,11 +6,21 @@ class FormSetField(forms.Field): widget = FormSetWidget - def __init__(self, formset_class, template=None, *args, **kwargs): + def __init__(self, formset_class, template=None, formset_class_attrs=None, *args, **kwargs): self.formset_class = formset_class + self._formset_class_attrs = formset_class_attrs or {} self.template = template or 'formsetfield/formsetfield.html' super(FormSetField, self).__init__(*args, **kwargs) + @property + def formset_class_attrs(self): + return self._formset_class_attrs + + @formset_class_attrs.setter + def formset_class_attrs(self, value): + self._formset_class_attrs = value + self.widget.attrs['formset_class_attrs'] = value + def validate(self, value): if not value.is_valid(): raise forms.ValidationError(self.error_messages['invalid']) @@ -19,4 +29,4 @@ def clean(self, value): return super(FormSetField, self).clean(value).cleaned_data def widget_attrs(self, widget): - return {'formset_class': self.formset_class, 'template': self.template} + return {'formset_class': self.formset_class, 'template': self.template, 'formset_class_attrs': self.formset_class_attrs} diff --git a/formsetfield/widgets.py b/formsetfield/widgets.py index 0421169..e7924ea 100644 --- a/formsetfield/widgets.py +++ b/formsetfield/widgets.py @@ -7,8 +7,8 @@ class FormSetWidget(forms.Widget): def render(self, name, value, attrs=None): if value is None: - value = self.attrs['formset_class'](prefix=name) - return render_to_string(self.attrs['template'], {'formset': value}) + value = self.attrs['formset_class'](prefix=name, **self.attrs['formset_class_attrs']) + return render_to_string(self.attrs['template'], {'formset': value, 'formset_args': self.attrs['formset_class_attrs']}) def value_from_datadict(self, data, files, name): - return self.attrs['formset_class'](data, files, prefix=name) + return self.attrs['formset_class'](data, files, prefix=name, **self.attrs['formset_class_attrs'])