django-bootstrap-modal-forms icon indicating copy to clipboard operation
django-bootstrap-modal-forms copied to clipboard

FormValidationMixin shadows setting of self.object (defined in BaseCreateView)

Open sisch opened this issue 1 year ago • 1 comments
trafficstars

I was tracking down, why self.object wasn't available in my view (deriving from BSModalCreateView) during get_success_url(), as in Django's BaseCreateView.form_valid(form) the default behaviour is to set self.object = form.save().

However, in django-bootstrap-modal-forms form_valid is shadowed by FormValidationMixin which currently fails to either

  • replicate the behaviour or
  • call the super() function.

sisch avatar Apr 05 '24 20:04 sisch

Having the same issue, found any workarounds yet? For now I have to revert back to an older version.

jweiskittel avatar Aug 09 '24 16:08 jweiskittel

Hi, comrades. i'm having the same issue, but my solution is inherit mixin FormValidationMixin, based on this post mr' s Sisch. As an example:

from bootstrap_modal_forms.mixins import PassRequestMixin, FormValidationMixin, is_ajax


class MyFormValidationMixin(FormValidationMixin):

    def form_valid(self, form):
        isAjaxRequest = is_ajax(self.request.META)
        asyncUpdate = self.request.POST.get('asyncUpdate') == 'True'

        if isAjaxRequest:
            if asyncUpdate:
                self.object = form.save() #here changed for solution
            return HttpResponse(status=204)

        self.object = form.save() #here changed for solution
        messages.success(self.request, self.get_success_message())
        return HttpResponseRedirect(self.get_success_url())

class OrderCreateOperatorView(LoginRequiredMixin, PassRequestMixin, MyFormValidationMixin, CreateView):
    template_name = 'orders/create_order_operator.html'
    form_class = OrderCreateOperatorForm
    success_message = 'Заказ оформлен'
    success_url = reverse_lazy('contacts:n_contacts')

    def get_success_url(self):
        return reverse_lazy('orders:OrderDetailView', kwargs={'pk': self.object.pk})

It gives control over the object by self.object

MaxSpacer avatar Sep 01 '24 12:09 MaxSpacer