Created
December 15, 2014 21:32
Formset Mixins
This file contains 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 FormsetMixin(object): | |
""" Use for a form + 1 formset """ | |
detail_form_class = None | |
def get_detail_form_class(self): | |
""" | |
Returns the detail form class to use in this view | |
""" | |
return self.detail_form_class | |
def get_detail_form(self, detail_form_class): | |
""" | |
Returns an instance of the detail form to be used in this view. | |
""" | |
return detail_form_class(**self.get_form_kwargs()) | |
def get(self, request, *args, **kwargs): | |
""" | |
Handles GET requests and instantiates blank versions of the form | |
and its inline formset. | |
Expects self.object to be set by a downstream method | |
""" | |
form_class = self.get_form_class() | |
form = self.get_form(form_class) | |
detail_form_class = self.get_detail_form_class() | |
detail_form = self.get_detail_form(detail_form_class) | |
return self.render_to_response( | |
self.get_context_data(form=form, | |
detail_form=detail_form)) | |
def post(self, request, *args, **kwargs): | |
""" | |
Handles POST requests, instantiating a form instance and its inline | |
formset with the passed POST variables and then checking them for | |
validity. | |
Expects self.object to be set by a downstream method | |
""" | |
form_class = self.get_form_class() | |
form = self.get_form(form_class) | |
detail_form_class = self.get_detail_form_class() | |
detail_form = self.get_detail_form(detail_form_class) | |
if (form.is_valid() and detail_form.is_valid()): | |
return self.form_valid(form, detail_form) | |
else: | |
return self.form_invalid(form, detail_form) | |
def form_valid(self, form, detail_form): | |
""" | |
Called if all forms are valid. Creates a master instance along with | |
details and then redirects to a | |
success page. | |
""" | |
self.object = form.save() | |
detail_form.instance = self.object | |
detail_form.save() | |
return HttpResponseRedirect(self.get_success_url()) | |
def form_invalid(self, form, detail_form): | |
""" | |
Called if a form is invalid. Re-renders the context data with the | |
data-filled forms and errors. | |
""" | |
return self.render_to_response( | |
self.get_context_data(form=form, | |
detail_form=detail_form)) | |
class FormsetCreateMixin(FormsetMixin): | |
def get(self, request, *args, **kwargs): | |
""" | |
Set self.object to None | |
""" | |
self.object = None | |
return super(FormsetCreateMixin,self).get(request, *args, **kwargs) | |
def post(self, request, *args, **kwargs): | |
""" | |
Set self.object to None | |
""" | |
self.object = None | |
return super(FormsetCreateMixin,self).post(request, *args, **kwargs) | |
class FormsetUpdateMixin(FormsetMixin): | |
def get(self, request, *args, **kwargs): | |
""" | |
Get self.object | |
""" | |
self.object = self.get_object() | |
return super(FormsetUpdateMixin,self).get(request, *args, **kwargs) | |
def post(self, request, *args, **kwargs): | |
""" | |
Get self.object | |
""" | |
self.object = self.get_object() | |
return super(FormsetUpdateMixin,self).post(request, *args, **kwargs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment