How do I update an already existing row when using ModelForms?

The django docs give a simple example of how to create "a form to change an existing [[entity]]":

>>> article = Article.objects.get(pk=1)
>>> form = ArticleForm(instance=article)

If as it seems you want to use the same flow both for inserting new objects and changing existing ones, you'll have to instantiate the form separately depending on whether looking for the primary key succeeds (existing object) or fails (new object)!-)


Here is a more complete solution not using any Class based views, bringing together the other answers and comments on this page.

I have it working as a reply to a jquery ajax.

def save_product(request):                                                                       
    if request.method == "POST":                                                                 
        # first get the model pk we are looking for
        postpk = request.POST.get('pk', None)

        # get the model from the db                                                
        model, created = Product.objects.get_or_create(pk = postpk)                          

        # create the from based on the model, but with the 
        # request data overriding the model data                                                 
        form = ProductForm(request.POST, instance = model)

        # save if valid                                       
        if form.is_valid():                                                                      
            form.save()                                                                          
            return HttpResponse("saved")                                         
        else:                             
            # will go to the the ajax error: data.responseText                                                       
            return HttpResponseNotFound("%s" % (form.errors))                                    
    else:                                                                                        
        return HttpResponseNotFound('eh? this was not a Post?')   

To update an existing row (or object in ORM-speak), you have to tell the ModelForm what instance to use when instantiating it:

f = DeviceModelForm(request.POST, instance=myobject)

I'm not sure where you get myobject from using piston, though, but your question seems to imply that you solved that particular problem already.