从asteapie的资源中删除列表端点

我的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时,出现错误


它正在尝试到达缺少的列表端点。我该如何摆脱呢?


万千封印
浏览 198回答 2
2回答

小怪兽爱吃肉

class UserResource(ModelResource):&nbsp; &nbsp; class Meta:&nbsp; &nbsp; &nbsp; &nbsp; queryset = User.objects.all()&nbsp; &nbsp; &nbsp; &nbsp; fields = ['email', 'name']&nbsp; &nbsp; &nbsp; &nbsp; authentication = MultiAuthentication(SessionAuthentication(), BasicAuthentication())&nbsp; &nbsp; &nbsp; &nbsp; authorization = Authorization()&nbsp; &nbsp; &nbsp; &nbsp; list_allowed_methods = []&nbsp; &nbsp; &nbsp; &nbsp; detail_allowed_methods = ['get']&nbsp; &nbsp; def dispatch_list(self, request, **kwargs):&nbsp; &nbsp; &nbsp; &nbsp; return self.dispatch_detail(request, **kwargs)&nbsp; &nbsp; def obj_get(self, bundle, **kwargs):&nbsp; &nbsp; &nbsp; &nbsp; '''&nbsp; &nbsp; &nbsp; &nbsp; Always returns the logged in user.&nbsp; &nbsp; &nbsp; &nbsp; '''&nbsp; &nbsp; &nbsp; &nbsp; return bundle.request.user&nbsp; &nbsp; def get_resource_uri(self, bundle_or_obj=None, url_name='api_dispatch_list'):&nbsp; &nbsp; &nbsp; &nbsp; bundle_or_obj = None&nbsp; &nbsp; &nbsp; &nbsp; try:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return self._build_reverse_url(url_name, kwargs=self.resource_uri_kwargs(bundle_or_obj))&nbsp; &nbsp; &nbsp; &nbsp; except NoReverseMatch:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return ''
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python