我的api上有一个总是返回登录用户的资源。资源是只读的。我希望列表uri充当详细信息uri,并删除详细信息网址。
因此,/api/v1/user/将返回登录的用户,其他任何URL都会失败。这是我为实现这一目标所做的:
class UserResource(ModelResource):
class Meta:
queryset = User.objects.all()
fields = ['email', 'name']
authentication = MultiAuthentication(SessionAuthentication(), BasicAuthentication())
authorization = Authorization()
list_allowed_methods = []
detail_allowed_methods = ['get']
def base_urls(self):
'''
The list endpoint behaves as the list endpoint.
'''
return [
url(r"^(?P<resource_name>%s)%s$" % (self._meta.resource_name, trailing_slash()), self.wrap_view('dispatch_detail'), name="api_dispatch_detail"),
url(r"^(?P<resource_name>%s)/schema%s$" % (self._meta.resource_name, trailing_slash()), self.wrap_view('get_schema'), name="api_get_schema")
]
def obj_get(self, bundle, **kwargs):
'''
Always returns the logged in user.
'''
return bundle.request.user
def get_resource_uri(self, bundle_or_obj=None, url_name='api_dispatch_detail'):
bundle_or_obj = None
try:
return self._build_reverse_url(url_name, kwargs=self.resource_uri_kwargs(bundle_or_obj))
except NoReverseMatch:
return ''
我之所以使用base_urls()而不是prepend_urls()因为我想删除其他网址。
它工作正常,但是当我点击/api/v1/url时,出现错误
它正在尝试到达缺少的列表端点。我该如何摆脱呢?
小怪兽爱吃肉
相关分类