我尝试在Django应用程序中获取有关经过身份验证的用户的详细信息。
为此,我创建了一个新资源:
class MyUserResource(ModelResource):
class Meta:
queryset = ReaderUser.objects.all()
resource_name = 'me'
list_allowed_methods = []
detail_allowed_methods = ['get']
authorization = Authorization()
authentication = SessionAuthentication()
excludes = ('password', 'id', 'is_active', 'is_admin', 'last_login')
def apply_authorization_limits(self, request, object_list):
print request.user
return object_list.filter(pk=request.user.pk)
def prepend_urls(self):
return [
url(r"^(?P<resource_name>%s)/$" % self._meta.resource_name, self.wrap_view('dispatch_detail'), name="api_dispatch_detail"),
]
当我使用/api/me/?format=json
调用我的API时
我得到了以下内容:More than one resource is found at this URI.
我也试过没有prepend_urls。
我不明白的是print
语句永远不会在方法apply_authorization_limits
关于我做错了什么的提示?
答案 0 :(得分:4)
我找到了两种解决问题的方法:
第一个是两个创建我自己的授权。
就我而言,以下内容:
from tastypie.authorization import Authorization
class SimpleReaderAuthorization(Authorization):
def read_list(self, object_list, bundle):
return object_list.filter(email=bundle.request.user.email)
我只需要更新我的资源:
class MyUserResource(ModelResource):
class Meta:
queryset = ReaderUser.objects.all()
resource_name = 'me'
list_allowed_methods = ['get']
authorization = SimpleReaderAuthorization()
authentication = SessionAuthentication()
excludes = ('password', 'id', 'is_active', 'is_admin', 'last_login')
另一种简单的方法是执行以下操作,如documentation。
中所示def get_object_list(self, request):
return super(YourResource, self).get_object_list(request).filter(pk=request.user.pk)
结论:我选择了第二个,因为它更简洁。
答案 1 :(得分:0)
文档没有显示,但apply_authorization_limits设置为已弃用。 不仅被弃用,它还被this commit从2013年2月的资源生命周期中剔除。这就是为什么它不再被召唤的原因。
当时更新的文档为here,自那时起没有太大变化。
将您的tastypie版本检查为&#39; read_list&#39;由updated documentation推荐。
def read_list(self, object_list, bundle):
"""
Returns a list of all the objects a user is allowed to read.
Should return an empty list if none are allowed.
Returns the entire list by default.
"""
return object_list
您的代码更改将如下所示:
from tastypie.authorization import Authorization
from tastypie.exceptions import Unauthorized
class MyAuthorization(Authorization):
def read_list(self, object_list, bundle):
print request.user
return object_list.filter(pk=bundle.request.user.pk)
class MyUserResource(ModelResource):
class Meta:
queryset = ReaderUser.objects.all()
resource_name = 'me'
list_allowed_methods = []
detail_allowed_methods = ['get']
authorization = MyAuthorization()
authentication = SessionAuthentication()
excludes = ('password', 'id', 'is_active', 'is_admin', 'last_login')
def prepend_urls(self):
return [
url(r"^(?P<resource_name>%s)/$" % self._meta.resource_name, self.wrap_view('dispatch_detail'), name="api_dispatch_detail"),
]
或者,您可以使用资源级别授权(在您的情况下更简单):
class MyUserResource(ModelResource):
class Meta:
queryset = ReaderUser.objects.all()
resource_name = 'me'
list_allowed_methods = []
detail_allowed_methods = ['get']
authentication = SessionAuthentication()
excludes = ('password', 'id', 'is_active', 'is_admin', 'last_login')
def authorized_read_list(self, object_list, bundle):
print request.user
return object_list.filter(pk=bundle.request.user.pk)
def prepend_urls(self):
return [
url(r"^(?P<resource_name>%s)/$" % self._meta.resource_name, self.wrap_view('dispatch_detail'), name="api_dispatch_detail"),
]
请注意,不需要授权&#39;在这种情况下,meta。
使用&#39;授权&#39;如果您有一种在共享同一授权类的不同资源上应用权限的通用和通用方法,那么meta是很好的。