[Answered ]-Adding work inside UpdateView before template is rendered

2πŸ‘

βœ…

It is debatable whether get_success_url is the right place to do this. form_valid is probably the better place for it.

def form_valid(self, form):
    instance = form.save(commit=False)
    instance.A = instance.B + instance.C
    return super(UpdateMyModelView, self).form_valid(form)    

This way you are saving the model only once. If you tried to do it in get_success_url, the model instance has already been saved in the form_valid method of the super class.

def get_success_url(self):
    # note self.object is the model instance
    self.object.A = self.object.B + self.object.C  
    return self.request.POST.get('next', '/default-url/')
πŸ‘€e4c5

Leave a comment