Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to pass custom arguments to target FormSet #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions formsetfield/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
Expand All @@ -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}
6 changes: 3 additions & 3 deletions formsetfield/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'])