My model has a required foreign key to the Users model. On the form submit I'd like to save the logged in user as the submitter.
我的模型具有Users模型所需的外键。在表单提交上,我想将登录用户保存为提交者。
In my view:
在我看来:
def myview(request):
if request.method == 'POST':
form = MyForm(request.POST)
myuser = User.objects.get(id=request.user.id)
form.submitter_field = myuser
if form.is_valid()
#this prints a failed message saying submitter_field is required
1 个解决方案
#1
0
Something like this may work for you. You can get the user id from the request session without running the user query.
这样的事可能适合你。您可以从请求会话中获取用户标识,而无需运行用户查询。
if form.is_valid():
f = form.save(commit=false)
f.submitter_field = request.user
f.save()
#1
0
Something like this may work for you. You can get the user id from the request session without running the user query.
这样的事可能适合你。您可以从请求会话中获取用户标识,而无需运行用户查询。
if form.is_valid():
f = form.save(commit=false)
f.submitter_field = request.user
f.save()