13

I am using django update view for my model/records editing stuff like below

class EditProductView(LoginRequiredMixin, UpdateView):
    model = Product
    def get_template_names(self):

        return ['website/product/edit_product.html']

    def get_success_url(self):

        return reverse('product_details', args=[self.kwargs['pk']])

    def get_context_data(self, **kwargs):
        publisher = Publisher.objects.get(product__id=self.kwargs['pk']) 
        context = super(EditProductView, self).get_context_data(**kwargs)
        context.update(
           {
            'publisher':publisher,
            }
          )
        return context        

edit_product =  EditProductView.as_view()   

So what all i want/trying to do is alter(add some data, edit already submitted data according to website functionality) the POST data before submitting to form,

So i know that UpdateView has some method def def post(self, request, *args, **kwargs): , but i dont know exactly how to use it

Suppose below is the request.POST data i am getting

<QueryDict: {u'product_name': [u'Biscuit'], u'product_price': [u'1000'], u'product_tag': [u'']}>

So now i want to alter the above QueryDict and if the value of product_tag was empty i need assign some default one and submit with latest querdict

Also i know that Querydict is mutable, but because of sure i need to edit the POST data, before submitting/saving to database, i need to make that querydict as dict, then edit it, and convert back to querdict

So after all whats my question is

  1. How can we alter the POST data in UpdateView before submitting/saving to database
  2. Is the post method heplful?

1 Answer 1

13

The QueryDict is mutable after you create its .copy(). See the docs.

Update Example:

class SomeUpdateView(UpdateView):

    def post(self, request, **kwargs):
        request.POST = request.POST.copy()
        request.POST['some_key'] = 'some_value'
        return super(SomeUpdateView, self).post(request, **kwargs)

Here is much broader discussion about the topic.

Furthermore, shouldn't this be done in ModelForm subclass? You're certainly aware you can set custom form as a form_class in UpdateView. Such a logic usually needs unit tests and it's much easier to unit test logic which sits in the form.

4
  • actually, i just want don't want to use the ModelForm as already facing some issues with ModelForms in my case. So finally can we edit the post data in the UpdateView above ? Dec 3, 2013 at 9:18
  • k that was helpful, but what if i need to replace the request.POST with entire new querydict ? Is there any way to replace request.POST with entire new querydict ? like request.POST = new_querdict Dec 3, 2013 at 9:35
  • Yeah. request.POST = QueryDict(). :) Dec 5, 2013 at 10:29
  • I'm using form_valid. What do I do? Jun 13, 2022 at 6:28

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.