1
我已經實現了django項目,它包含了tastypie應用程序。客戶端可以將請求發送到CRUD操作。獲取Django-tastypie認證的用戶詳細信息
我在訪問登錄的用戶詳細信息時遇到問題。在這裏我試圖實現BasicAuthentication + TokenBased身份驗證。
這是我的自定義實現
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)
有什麼想法?