0

我已经知道如何在 sweetpie 中创建 ModelResource。例如,我在 resources.py 中有 UserResource,在 models.py 中有 User。但是在views.py 中,我有一个名为match_user 的视图,它获取所有用户的列表并匹配到request.user。它返回一个名为 mymatch.html 的 render_to_response html。一切都在浏览器上运行,但我想为这个特定的 match_user 创建一个 API。我怎样才能做到这一点?

谢谢

4

1 回答 1

-1

我认为以下内容回答了您的问题:

class UserResource(ModelResource):
    class Meta:
        queryset = User.objects.all()
        resource_name = "user"
        authentication = SessionAuthentication()

    # User is only authorized to view his own details   
    def apply_authorization_limits(self, request, object_list):
        return object_list.filter(pk=request.user.pk)

如果用户具有活动会话并且已经登录,则会话身份验证将起作用。有关更多身份验证选项,请参阅https://django-tastypie.readthedocs.org/en/latest/authentication_authorization.html?highlight=authentication#authentication-options

于 2012-12-17T19:10:28.043 回答