我试图在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方法中执行
对我做错了什么有什么暗示吗?
发布于 2013-09-24 17:46:04
我找到了两种方法来解决我的问题:
第一个是创建我自己的授权。
就我而言,以下是:
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')另一种简单的方法是执行以下操作,如文档中所示。
def get_object_list(self, request):
return super(YourResource, self).get_object_list(request).filter(pk=request.user.pk)结论:我选择了第二个,因为它更干净,简单。
发布于 2015-12-27 12:07:58
文档没有显示,但是apply_authorization_limits被设置为不推荐使用。它不仅受到反对,还被此承诺于2013年2月从资源生命周期中剥离出来。所以不再叫它了。
当时更新的文档是这里,从那以后没有发生太大的变化。
检查您的品位版本,因为'read_list‘是由更新的文件推荐的。
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"),
]请注意,在本例中不需要“授权”元。
如果您有一种通用的方法对共享相同授权类的不同资源应用权限,那么使用“授权”元是很好的。
https://stackoverflow.com/questions/18969707
复制相似问题