2012-11-13 93 views
0

我已经知道如何在tastypie中创建ModelResource。例如,我在resources.py中使用UserResource,在models.py中使用User。然而,在views.py中,我有一个名为match_user的视图,它将所有用户的列表和request.user相匹配。它返回一个名为mymatch.html的render_to_response html。一切工作在浏览器上,但我想为这个特定的match_user创建一个API。我怎样才能做到这一点?建立tastypie资源?

谢谢

+1

您是否需要过滤API端点或序列化View中的资源? – dokkaebi

回答

-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