On 2008-01-25 19:18:22 -0700, "Vance Dubberly" <[EMAIL PROTECTED]> said:

> 
> ok so this ones got to be really simple and I've go to be really stupid.
> 
> I want to update a model of something I already have in the database
> so I made a ModelForm that doesn't do anything special for it and I
> use that form to generate a form in a template using the normal
> form.as_table method.  Everything prints out alright except for one
> thing,  there is no id.  "Hmm this'll be troublesome.", I think. "How
> am I gunna know what record to update when the post comes in?" So I
> figure I'm on crack and obviously don't know anything about http or
> databases any of that stuff. Maybe django has some magic foo that's
> over my head. So I print out the form, click the submit button, and
> now there are two copies of this object in the database.  So having
> read all the docs and book I now come to ya'll.  How am i supposed to
> do a simple update using newforms?

It sounds like you are not passing in the instance to the form in the 
if request.method == "POST" condition. There is no need for an ID in 
the form because you are probably POSTing to the same URL with ID 
there. Check the following code:

def view(request, pk):
    instance = get_object_or_404(MyModel, pk=pk)
    if request.method == "POST":
        form = MyModelForm(request.POST, request.FILES, instance=instance)
        if form.is_valid():
            form.save()
    else:
        form = MyModelForm(instance=instance)
    return render_to_response("mytemplate.html", {
        "form": form,
    }, context_instance=RequestContext(request))

-- 
Brian Rosner
http://oebfare.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-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to