Get the currently saved object in a view in Django

Posted by mridang on Stack Overflow See other posts from Stack Overflow or by mridang
Published on 2010-04-13T15:41:32Z Indexed on 2010/04/13 15:43 UTC
Read the original article Hit count: 597

Filed under:
|
|

Hi,

I has a Django view which is accessed through an AJAX call. It's a pretty simple one — all it does is simply pass the request to a form object and save the data. Here's a snippet from my view:

form = AddSiteForm(request.user, request.POST)
if form.is_valid():
    obj = form.save(commit=False)
    obj.user = request.user
    obj.save()
    data['status'] = 'success'
    data['html'] = render_to_string('site.html', locals(), context_instance=RequestContext(request))
    return HttpResponse(simplejson.dumps(data), mimetype='application/json')

How do I get the currently saved object (including the internally generated id column) and pass it to the template?

Any help guys?

Mridang

© Stack Overflow or respective owner

Related posts about django

Related posts about django-views