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)

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/e993e258-e6ef-4ed1-b3bc-93f320313368%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to