Friday, November 23, 2012

Re: Editing model instance

For starters, I see more than one thing wrong with your code. I hope thats not the one your actually using and it was just a typo when you asked the question.
+ jform = editJobForm(request.POST, instance=job) # job has not being defined.
Also when you instantiate your form, you want to use and instance of the class your want to use, for instance.
    clientjob = ClientEditJob.objects.get(job_id =query) 
    job = EditJob.objects.get(job_id=query)

Right now you using this: clientjob = ClientJob.objects.get(job_id = query). ClientJob is not a model for any of the two forms you are using.

Let me know if I was of any help,
Victor Rocha


On Thursday, November 22, 2012 5:59:11 AM UTC-5, sandy wrote:
I edit value of a table using model instance, however after editing I
want the values to be saved in some other table with same structure.
For this to happen I have used following code in views.py :

def editjob(request):
                clientjob = ClientJob.objects.get(job_id = query)
                    if request.method == "POST":
                        jform = editJobForm(request.POST, instance=job)
                        sform = editClientJobForm(request.POST, instance=clientjob)
                        if jform.is_valid() and sform.is_valid():
                                jform.save()
                                    sform.save()
                                    return
render_to_response('tcc/succes.html',context_instance=RequestContext(request))
                else:        
                        jform = editJobForm(instance=job)
                        sform = editClientJobForm(instance=clientjob)
                return render_to_response('tcc/edit_job.html', {'jform':
jform,'sform':sform},context_instance=RequestContext(request))

where :
class editJobForm(forms.ModelForm):
        class Meta :
                model = EditJob
                exclude= ['client','job_no','id']

class editClientJobForm(forms.ModelForm):

        class Meta :
                model = ClientEditJob
                exclude= ['job']

However this code saves the value in same instance itself. What I want
is to get old values from table: Job and ClientJob and then after
editing get saved in tables: EditJob and ClentEditJob.
Is this possible? Your help will be appreciated.
Thank you.

--
Sandeep Kaur
E-Mail: mkaur...@gmail.com
Blog: sandymadaan.wordpress.com

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/H6ly_fe2CXEJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment