Need some help with updating a database record from forms cleaned data

14 views
Skip to first unread message

progm...@gmail.com

unread,
Dec 15, 2018, 10:58:36 PM12/15/18
to Django users
Hi I am trying to update a record. Form.save() worked but it created a new record and I have yet to figure out how to update a database record in views.py. Below are the 2 views related to what I'm doing. On one page I list customers to be chosen for update. When a customer is chosen, the pk is posted back and the user is redirected to the edit page. Any idea how I can update the db record with the form's cleaned data? Thanks

# Allows for the customer record to be selected 
# Then directs to an edit form
class listcustomers(ListView):
    model = dbCustomer
    template_name = "mysite/admin_listcustomers.html"
    
    queryset = dbCustomer.objects.all()
    context_object_name = 'customerlist'   
    paginate_by= 1

    def post(self,request):
        for key in request.POST:
            if "customer_" in key:
                print(key, request.POST[key])
                if request.POST[key] == "Edit":
                    pk = re.findall('\d', key)
                    if pk:
                        # returned pk has been verified 
                        # dump cusomer id into a session variable
                        # and redirect for editing
                        request.session['customer_pk'] =int(pk[0])
                        return redirect("editcustomer")
                else:
                    pass
        return render(request,self.template_name,)

# This is where the selected customer gets the form to allow editing
def editcustomer(request):
    template_name='mysite/admin_editcustomer.html'
    pk = request.session['customer_pk']
    qs = dbCustomer.objects.get(id=pk)

    # Posted for saving
    if request.method=="POST":
        form = frmCustomer(request.POST)
        if form.is_valid():
            # here I tried form.save() hoping frmcustomer would be bound
            # to the record the queryset pulled, it isn't.
            # form.save() just created a new record
            # I have the pk. 
            # How do I simply update the record from the form.cleaned data
            # This did not work
            dbCustomer.objects.filter(pk).update(form.cleaned_data)
            print(form.cleaned_data["contactfn"]) #Cleaned data is there.

            # If updated, get rid of currect session variable
            request.session['customer_pk']=''
            # return to the customer list
            return redirect("listcustomers")
        else:
            args = {"form":form,}
            return render(request,template_name,args)    
    else:
        # Getting for editing
        
        form = frmCustomer(initial=model_to_dict(qs))
        args = {'form':form,}
        return render(request,template_name,args)

progm...@gmail.com

unread,
Dec 15, 2018, 11:11:10 PM12/15/18
to Django users
While I'm waiting on a response, I will try an update view.
Reply all
Reply to author
Forward
0 new messages