Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
sisch opened this issue Apr 5, 2024 · 2 comments · May be fixed by #240
Open

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

sisch opened this issue Apr 5, 2024 · 2 comments · May be fixed by #240

Comments

@sisch
Copy link

sisch commented Apr 5, 2024

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.
@jweiskittel
Copy link

jweiskittel commented Aug 9, 2024

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

@MaxSpacer
Copy link

MaxSpacer commented Sep 1, 2024

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

MarkVergunst added a commit to MarkVergunst/django-bootstrap-modal-forms that referenced this issue Sep 17, 2024
@MarkVergunst MarkVergunst linked a pull request Sep 17, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants