class ReciboCreateView(CreateView, LoginRequiredMixin): """Permite crear un :class:`Recibo` al mismo tiempo que se crea una :class:`Persona`, utiliza un formulario simplificado que requiere unicamente indicar el nombre y apellidos del cliente, que aún así son opcionales. """ model = Recibo def dispatch(self, request, *args, **kwargs): self.persona = Persona() self.ReciboFormset = inlineformset_factory(Persona, Recibo, form=ReciboForm, fk_name='cliente', extra=1) return super(ReciboCreateView, self).dispatch(request, *args, **kwargs) def get_form(self, form_class=None): self.persona_form = PersonaForm(instance=self.persona, prefix='persona') self.persona_form.helper.form_tag = False formset = self.ReciboFormset(instance=self.persona, prefix='recibo') return formset def get_context_data(self, **kwargs): context = super(ReciboCreateView, self).get_context_data(**kwargs) context['persona_form'] = self.persona_form return context def post(self, request, *args, **kwargs): self.form = PersonaForm(request.POST, request.FILES, instance=self.persona, prefix='persona') self.formset = self.ReciboFormset(request.POST, request.FILES, instance=self.persona, prefix='recibo') if self.form.is_valid() and self.formset.is_valid(): self.form.save() instances = self.formset.save() for instance in instances: self.recibo = instance self.recibo.cajero = self.request.user self.recibo.save() return self.form_valid(self.formset) else: return self.form_invalid(self.formset) def get_success_url(self): return reverse('invoice-view-id', args=[self.recibo.id])
class ReciboCreateView(CreateView, LoginRequiredMixin): """Permite crear un :class:`Recibo` al mismo tiempo que se crea una :class:`Persona`, utiliza un formulario simplificado que requiere unicamente indicar el nombre y apellidos del cliente, que aún así son opcionales. """ model = Recibo def dispatch(self, request, *args, **kwargs): self.persona = Persona() self.ReciboFormset = inlineformset_factory(Persona, Recibo, form=ReciboForm, fk_name='cliente', extra=1) return super(CreateView, self).dispatch(request, *args, **kwargs) def get_form(self, form_class): self.persona_form = PersonaForm(instance=self.persona, prefix='persona') self.persona_form.helper.form_tag = False formset = self.ReciboFormset(instance=self.persona, prefix='recibo') return formset def get_context_data(self, **kwargs): context = super(ReciboCreateView, self).get_context_data(**kwargs) context['persona_form'] = self.persona_form return context def post(self, request, *args, **kwargs): self.form = PersonaForm(request.POST, request.FILES, instance=self.persona, prefix='persona') self.formset = self.ReciboFormset(request.POST, request.FILES, instance=self.persona, prefix='recibo') if self.form.is_valid() and self.formset.is_valid(): self.form.save() instances = self.formset.save() for instance in instances: self.recibo = instance self.recibo.cajero = self.request.user self.recibo.save() return self.form_valid(self.formset) else: return self.form_invalid(self.formset) def get_success_url(self): return reverse('invoice-view-id', args=[self.recibo.id])
def get_form(self, form_class=None): self.persona_form = PersonaForm(instance=self.persona, prefix='persona') self.persona_form.helper.form_tag = False formset = self.ReciboFormset(instance=self.persona, prefix='recibo') return formset
def post(self, request, *args, **kwargs): self.form = PersonaForm(request.POST, request.FILES, instance=self.persona, prefix='persona') self.formset = self.ReciboFormset(request.POST, request.FILES, instance=self.persona, prefix='recibo') if self.form.is_valid() and self.formset.is_valid(): self.form.save() instances = self.formset.save() for instance in instances: self.recibo = instance self.recibo.cajero = self.request.user self.recibo.save() return self.form_valid(self.formset) else: return self.form_invalid(self.formset)