is anybody know how to log the username who is currently loged in in form.py
that won't help forms processing.
TokenMacGuy
2009-06-10 07:10:33
You could very well store it in a hidden form field.I agree Van Gale's approach is djangoic and elegant. But one simple solution definitely is to store a hidden form element containing userid.
Lakshman Prasad
2009-06-10 07:53:43
+5
A:
You need an __init__
method on your form, and pass request.user as an argument.
For example:
class MyModelForm(forms.ModelForm):
# Notice the custom argument 'user'
def __init__(self, user, *args, **kwargs):
super(MyModelForm, self).__init__(*args, **kwargs)
# do something here with user, such as calling log.debug()
class Meta:
model = MyModel
Now in your views.py, you create the form like this:
form = MyModelForm(request.user)
or, if you are processing POST request:
form = MyModelForm(request.user, request.POST)
Edit to update answer after comment.
To get the user in the admin site you need to add the following method to your admin model in admin.py:
def save_model(self, request, obj, form, change):
# Do something with request.user here
obj.save()
Of course, this means you only have access to the user during a save. If you need access to request.user for other parts of admin, such as filtering querysets, see this post by James Bennett for more info:
Van Gale
2009-06-09 12:12:20
I m not making a view for this .Can i do so in my admin.py'form =MyModelForm(reuest.user)i only have form.py and admin.py
ha22109
2009-06-10 05:32:27