我已经实现了 django 项目,它包括了美味的应用程序。客户端可以将请求发送到 CRUD 操作。
我在访问登录的用户详细信息时遇到问题。在这里,我正在尝试实现 BasicAuthentication + TokenBased Authentication。
这是我的自定义实现
from tastypie.authentication import Authentication
from tastypie.authorization import Authorization
class MyAuthentication(Authentication):
def is_authenticated(self, request, **kwargs):
return True
from django.contrib.sessions.models import Session
if 'sessionid' in request.COOKIES:
try:
s = Session.objects.get(pk=request.COOKIES['sessionid'])
except Exception,e:
return False
# get the user_id from the session
if '_auth_user_id' in s.get_decoded():
# try to find the user associated with the given sessionid
try:
u = User.objects.get(pk=s.get_decoded()['_auth_user_id'])
if u is not None:
return False
except Exception,e:
return False
else:
return False
else:
return False
def get_identifier(self, request):
return request.user.username
那么我的资源文件,我正在尝试访问该MyAuthenticate
课程。
class InvoiceResource(ModelResource):
user = fields.ForeignKey(UserResource, 'user')
class Meta:
user = User.objects.get(pk=1)
queryset = user.invoice_set.all()
resource_name = 'invoice'
authorization = Authorization()
authentication = MyAuthentication();
这就是问题出现了。我需要加载属于登录用户的所有发票。基本上我想将记录的用户 ID 作为此查询的参数传递。
user = User.objects.get(pk=1)
任何想法?。