Giter Club home page Giter Club logo

Comments (6)

bryannatali avatar bryannatali commented on June 28, 2024

I think its happening 'cause the .each() jquery function isnt correct

My View Class

class KeyResultEditView(BSModalUpdateView):
	model = CompanyKeyResult
	template_name = 'modals/edit_keyresult.html'
	form_class = CompanyKeyResultForm
	success_message = 'Resultado chave alterado com sucesso.'
	success_url = reverse_lazy('okr_company')

	def dispatch(self, request, *args, **kwargs):
		self.key_result = get_object_or_404(CompanyKeyResult, pk=kwargs['pk'])
		ProgressHistory.objects.create(history_company_key_result=self.key_result, date=self.key_result.date, percent=self.key_result.percent)

		return super().dispatch(request, *args, **kwargs)

Url
path('modals/edit-kr/<int:pk>', views.KeyResultEditView.as_view(), name='edit_key_result'),

Js
$(".edit-kr").each(function(){ $(this).modalForm({formURL: $(this).data('id')}); });

from django-bootstrap-modal-forms.

trco avatar trco commented on June 28, 2024

@bryannatali I would say that each function is not a problem. It's identical to the docs. What do you mean when saying that the form is submitted 3 times? It normal that it's submitted 2 times. Check #14.

from django-bootstrap-modal-forms.

bryannatali avatar bryannatali commented on June 28, 2024

okay, thank you for quick response.

When I submit the modal this ProgressHistory.objects.create(history_company_key_result=self.key_result, date=self.key_result.date, percent=self.key_result.percent) happens three times. So the model is created three times on db.

from django-bootstrap-modal-forms.

trco avatar trco commented on June 28, 2024

@bryannatali Ok, now I see. You defined a creation of another object inside of a dispatch method. Although it works and creates an object, my opinion is that you shouldn't use Django in this way. So you need to figure out how to move this code out of dispatch. When you POST the form dispatch is called. In case of my package two POSTs result in two dispatch calls, so this explains why two objects are created however I don't know where the third comes from. So reconsider your design and definitely don't abuse dispatch method.

from django-bootstrap-modal-forms.

bryannatali avatar bryannatali commented on June 28, 2024

Oh, thank you again!

Based on what you said I changed my ViewClass to:

class KeyResultEditView(BSModalUpdateView):
    model = CompanyKeyResult
    template_name = 'modals/edit_keyresult.html'
    form_class = CompanyKeyResultForm
    success_message = 'Resultado chave alterado com sucesso.'
    success_url = reverse_lazy('okr_company')

    def dispatch(self, request, *args, **kwargs):
        self.key_result = get_object_or_404(CompanyKeyResult, pk=kwargs['pk'])
        return super().dispatch(request, *args, **kwargs)

    def form_valid(self, form):
        try:
            ProgressHistory.objects.get(date=self.key_result.date, percent=self.key_result.percent)
        except ProgressHistory.DoesNotExist:
            ProgressHistory.objects.create(history_company_key_result=self.key_result, date=self.key_result.date, percent=self.key_result.percent)
        return super().form_valid(form)

and it works!

from django-bootstrap-modal-forms.

trco avatar trco commented on June 28, 2024

@bryannatali That's great. I'm glad you made it. Star 70 highly appreciated ;)

from django-bootstrap-modal-forms.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.