0

Possible Duplicate:
Django forms request.user

I'd like to update a logging model while saving a ModelForm, overriding the save() method of the ModelForm.
My problem is that I need, when populating the log, to add the username of the user who posted the form, and request.user is not available at this moment (which is normal, I'm in the ModelForm save() method...).

Is there any way to access to this value, other than adding an hidden input to recognize the author of the POST ?

Community
  • 1
  • 1
Dominique Guardiola
  • 3,431
  • 2
  • 22
  • 22

1 Answers1

0

You can do this from your view function.

def updateModel(request)
    if request.method == 'POST':
         form = MyForm(requst.POST)
         if form.is_valid():
             #do your logging here
             #log(request.user, "updated model")
             form.save()

The only issue you may have with this is if you have multiple views that use form.save(), then you'd need to always remember to log it.

jonescb
  • 22,013
  • 7
  • 46
  • 42
  • Yes, that's what I'm doing now, but the treatment is very long and I wanted to put it into the ModelForm to be able to use it in other views... – Dominique Guardiola Jan 21 '11 at 17:57