Skip to content

Instantly share code, notes, and snippets.

@tobiasmcnulty
Created February 5, 2017 15:19
Show Gist options
  • Save tobiasmcnulty/f51591e23df15b6d62930c8b4f749228 to your computer and use it in GitHub Desktop.
Save tobiasmcnulty/f51591e23df15b6d62930c8b4f749228 to your computer and use it in GitHub Desktop.
Django form-like object that wraps a related group of forms.
class FormGroup(object):
"""
Form-like object that wraps a related group of forms.
"""
def __init__(self, main_form, **other_forms):
self.main_form = main_form
self.other_forms = list(other_forms.values())
for k, v in other_forms.items():
setattr(self, k, v)
def is_valid(self):
return all([f.is_valid() for f in [self.main_form] + self.other_forms])
def save(self, *args, **kwargs):
obj = self.main_form.save(*args, **kwargs)
kwargs['main_obj'] = obj
for form in self.other_forms:
form.save(*args, **kwargs)
return obj
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment