Editing model instance

25 views
Skip to first unread message

Sandeep kaur

unread,
Nov 22, 2012, 5:58:16 AM11/22/12
to django-users
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

Sergiy Khohlov

unread,
Nov 22, 2012, 6:24:36 AM11/22/12
to django...@googlegroups.com
Have you tried to change instances to your new tables ?

2012/11/22 Sandeep kaur <mkaur...@gmail.com>:
> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to django-users...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
>

Sandeep kaur

unread,
Nov 22, 2012, 6:37:51 AM11/22/12
to django...@googlegroups.com
On Thu, Nov 22, 2012 at 4:54 PM, Sergiy Khohlov <skho...@gmail.com> wrote:
> Have you tried to change instances to your new tables ?
>
If I try instance of new table, I won't get the old values that I need to edit.

Sergiy Khohlov

unread,
Nov 22, 2012, 7:02:33 AM11/22/12
to django...@googlegroups.com
you can set values in form via form __init__ ....

2012/11/22 Sandeep kaur <mkaur...@gmail.com>:

Sandeep kaur

unread,
Nov 23, 2012, 2:16:38 AM11/23/12
to django...@googlegroups.com
On Thu, Nov 22, 2012 at 5:32 PM, Sergiy Khohlov <skho...@gmail.com> wrote:
> you can set values in form via form __init__ ....
>
Values of some other instance? How?

Victor Rocha

unread,
Nov 23, 2012, 8:55:06 AM11/23/12
to django...@googlegroups.com
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

Victor Rocha

unread,
Nov 23, 2012, 8:58:28 AM11/23/12
to django...@googlegroups.com
This would be the code I would like you to try:

def editjob(request): 
                clientjob = ClientEditJob.objects.get(job_id =query) #adjust query as needed
                job = EditJob.objects.get(job_id=query)#adjust query as needed

                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)) 

Thank you,
Victor Rocha

Sandeep kaur

unread,
Nov 23, 2012, 2:03:56 PM11/23/12
to django...@googlegroups.com
On Fri, Nov 23, 2012 at 7:25 PM, Victor Rocha <vico...@gmail.com> wrote:
> 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. gi

> clientjob = ClientEditJob.objects.get(job_id =query)
> job = EditJob.objects.get(job_id=query)
>
If I do this I won't get previously filled values of ClientJob table
which I need for editing. ClientEditJob table is empty.

> 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.
>
I think you haven't understood what I want. I don't say my code is
correct, Because if it would be, I would have got my results. Please
help me change it to correct one.
What I want to do :
Get instance of one table, which after editing get saved to some other table.
What I get :
I get values of one table and after editing it get saved to the same table. :(
Reply all
Reply to author
Forward
0 new messages