【发布时间】:2016-07-31 15:50:22
【问题描述】:
给定一个 Django 表单,我有以下save() 方法:
def save(self):
name = self.cleaned_data.get('name', None)
user = User.objects.get(1) #I want the session user ID from here
所以我按原样导入:
from django.http import HttpRequest as request
在上面我写的代码中
request.session.get('user_id') #for example
但是,Django 在运行时会抛出以下错误:
AttributeError: type object 'HttpRequest' has no attribute 'session'
有没有更好的方法来进行这种检索?
【问题讨论】: