私はdjangoプロジェクトを実装しました。これにはtastypieアプリが含まれています。クライアントは 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)
何か案が?。