Created
February 5, 2017 15:19
-
-
Save tobiasmcnulty/f51591e23df15b6d62930c8b4f749228 to your computer and use it in GitHub Desktop.
Django form-like object that wraps a related group of forms.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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