Re: form instance to update an object

2009-06-16 Thread Daniel Roseman

On Jun 16, 9:48 am, Genis Pujol Hamelink 
wrote:
> well, if you retrieve an object via GET modify something and then submit the
> new data you will create a POST request and I was wondering if I could test
> wether the object being saved was an existing one or not...
>
> greetings,
>
> Genis
>
OK, so you have the existing object. Now you can pass it in via the
instance parameter when you instantiate the form, as I stated above.
--
DR.
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: form instance to update an object

2009-06-16 Thread Genis Pujol Hamelink
well, if you retrieve an object via GET modify something and then submit the
new data you will create a POST request and I was wondering if I could test
wether the object being saved was an existing one or not...

greetings,

Genis

2009/6/16 Daniel Roseman 

>
> On Jun 15, 4:23 pm, Genis Pujol Hamelink 
> wrote:
> > yes, but what if it's a new object and not an existing one? How do I test
> > this? The request method will be POST, so form will be form =
> > MyForm(request.POST)... so if form.pk exists in the db how do I tell
> it's
> > editing an existing object?
> >
> > if request.method == 'POST':
> >  form = myform(request.POST)
> >  try:
> >instance = myobj.objects.get(id=form.id)
> >form = myform(request.POST, instance=instance)
> >if form.is_valid():
> >  form.save()
> >else:
> >   whatever goes here
> >  except:
> >form = myform(request.POST)
> > if form.is_valid():
> >  form.save()
> >else:
> >   whatever goes here
> >
> > Something like this or do u know a better way?
> >
>
> I don't really understand how a newly-entered item can exist in the
> database already. At the very least, saving the new item would give it
> a new PK, so it wouldn't be a duplicate, surely.
> --
> DR.
> >
>


-- 
Genís

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



Re: form instance to update an object

2009-06-16 Thread Daniel Roseman

On Jun 15, 4:23 pm, Genis Pujol Hamelink 
wrote:
> yes, but what if it's a new object and not an existing one? How do I test
> this? The request method will be POST, so form will be form =
> MyForm(request.POST)... so if form.pk exists in the db how do I tell it's
> editing an existing object?
>
> if request.method == 'POST':
>      form = myform(request.POST)
>      try:
>        instance = myobj.objects.get(id=form.id)
>        form = myform(request.POST, instance=instance)
>        if form.is_valid():
>          form.save()
>        else:
>           whatever goes here
>      except:
>        form = myform(request.POST)
>         if form.is_valid():
>          form.save()
>        else:
>           whatever goes here
>
> Something like this or do u know a better way?
>

I don't really understand how a newly-entered item can exist in the
database already. At the very least, saving the new item would give it
a new PK, so it wouldn't be a duplicate, surely.
--
DR.
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: form instance to update an object

2009-06-15 Thread Genis Pujol Hamelink
yes, but what if it's a new object and not an existing one? How do I test
this? The request method will be POST, so form will be form =
MyForm(request.POST)... so if form.pk exists in the db how do I tell it's
editing an existing object?

if request.method == 'POST':
 form = myform(request.POST)
 try:
   instance = myobj.objects.get(id=form.id)
   form = myform(request.POST, instance=instance)
   if form.is_valid():
 form.save()
   else:
  whatever goes here
 except:
   form = myform(request.POST)
if form.is_valid():
 form.save()
   else:
  whatever goes here

Something like this or do u know a better way?





2009/6/15 Daniel Roseman 

>
> On Jun 15, 3:20 pm, Genis Pujol Hamelink 
> wrote:
> > Hi list,
> >
> > I am trying to create a view to update objects in the db:
> >
> > http://dpaste.com/55546/
> >
> > but when posting the data, form.is_valid() fails because the object
> already
> > exists in the db. Is there a way to validate a form using an existing
> object
> > and then save it?
> >
> > regards,
> >
> > --
> > Genís
>
> Yes, pass the instance as a parameter to the form instantiation.
> form = MyForm(instance=instance)
> --
> DR.
> >
>


-- 
Genís

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



Re: form instance to update an object

2009-06-15 Thread Daniel Roseman

On Jun 15, 3:20 pm, Genis Pujol Hamelink 
wrote:
> Hi list,
>
> I am trying to create a view to update objects in the db:
>
> http://dpaste.com/55546/
>
> but when posting the data, form.is_valid() fails because the object already
> exists in the db. Is there a way to validate a form using an existing object
> and then save it?
>
> regards,
>
> --
> Genís

Yes, pass the instance as a parameter to the form instantiation.
form = MyForm(instance=instance)
--
DR.
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---