【问题标题】:Build tastypie resource in view?在视图中构建美味的资源?
【发布时间】:2012-11-13 07:59:00
【问题描述】:

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

谢谢

【问题讨论】:

  • 您需要过滤 API 端点,还是序列化视图中的资源?

标签: django django-views tastypie


【解决方案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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-25
    • 1970-01-01
    相关资源
    最近更新 更多