def form_valid(self, form): """ Runs after form validation. Creates the ``Target``, and creates any ``TargetName`` or ``TargetExtra`` objects, then runs the ``target_post_save`` hook and redirects to the success URL. :param form: Form data for target creation :type form: subclass of TargetCreateForm """ super().form_valid(form) extra = TargetExtraFormset(self.request.POST) names = TargetNamesFormset(self.request.POST) if extra.is_valid() and names.is_valid(): extra.instance = self.object extra.save() names.instance = self.object names.save() else: form.add_error(None, extra.errors) form.add_error(None, extra.non_form_errors()) form.add_error(None, names.errors) form.add_error(None, names.non_form_errors()) return super().form_invalid(form) logger.info('Target post save hook: %s created: %s', self.object, True) run_hook('target_post_save', target=self.object, created=True) return redirect(self.get_success_url())
def form_valid(self, form): super().form_valid(form) extra = TargetExtraFormset(self.request.POST) names = TargetNamesFormset(self.request.POST) if extra.is_valid() and names.is_valid(): extra.instance = self.object extra.save() names.instance = self.object names.save() else: form.add_error(None, extra.errors) form.add_error(None, extra.non_form_errors()) form.add_error(None, names.errors) form.add_error(None, names.non_form_errors()) return super().form_invalid(form) return redirect(self.get_success_url())