I want to store the name of the user who is currently logged into Django in a custom form. In the admin we can do so by writing modified_by=request.user.username, but how do I do this in my own form?
我想以自定义形式存储当前登录Django的用户的名称。在admin中我们可以通过编写modified_by = request.user.username来实现,但是如何以我自己的形式执行此操作?
1 个解决方案
#1
You can either define the __init__ method of your Form to accept a "request" parameter, and then pass that in from your view, or you can use the thread-locals hack.
您可以定义表单的__init__方法以接受“请求”参数,然后从视图中传入,或者您可以使用线程本地哈希。
#1
You can either define the __init__ method of your Form to accept a "request" parameter, and then pass that in from your view, or you can use the thread-locals hack.
您可以定义表单的__init__方法以接受“请求”参数,然后从视图中传入,或者您可以使用线程本地哈希。