在视图中构建tastypie资源?

时间:2012-11-13 07:59:00

标签: django django-views tastypie

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

谢谢

1 个答案:

答案 0 :(得分:-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