【问题标题】:Why there is need to add 'POST' method in both list_allowed_methods as well as detail_allowed_methods in Tastypie?为什么需要在 Tastypie 的 list_allowed_methods 和 detail_allowed_methods 中添加“POST”方法?
【发布时间】:2015-08-24 05:35:22
【问题描述】:
class EntryResource(ModelResource):
    user = fields.ForeignKey(UserResource, 'user')

class Meta:
    queryset = Entry.objects.all()
    list_allowed_methods = ['get','post']
    detail_allowed_methods = ['post']
    resource_name = 'myapp/entry'

为什么需要在 detail_allowed_methods 中添加“POST”,正如我评论 detail_allowed_methods “POST”工作正常..!

【问题讨论】:

    标签: python django rest api tastypie


    【解决方案1】:

    它工作正常,因为在注释掉 detail_allowed_methods 时,Tastypie 会回到它的默认值,即:

    ['get', 'post', 'put', 'delete', 'patch']
    

    所以,评论detail_allowed_methods 不会做任何事情。如果要禁用所有方法,请将其值设置为空列表:

    detail_allowed_methods = []
    

    See Tastypie docs.

    【讨论】:

    • True,根据文档,如果 detail_allowed_methods 被注释,则 detail_allowed_methods 的默认值将设置为 ['get', 'post', 'put', 'delete', 'patch']。但是为什么细节删除不起作用,因为它允许在 detail_allowed_methods
    【解决方案2】:

    发布到列表端点和发布到详细信息具有不同的含义。来自Tastypie docs

    要创建新的资源/对象,您将 POST 到资源的列表端点。尝试 POST 到详细端点在 REST 思维模式中具有不同的含义(意味着将资源添加为相同类型资源的子级)。

    如果您想为列表和详细信息使用相同的方法,您可以使用涵盖两者的allowed_methods。如果您想在两者之间使用不同的方法,则需要为每个端点指定允许的方法。

    正如@xyres 所说,仅为一个端点指定方法,将导致另一个端点回退到其默认值(允许所有方法)。

    【讨论】:

      猜你喜欢
      • 2011-10-08
      • 2015-01-04
      • 1970-01-01
      • 2016-12-27
      • 2018-10-19
      • 1970-01-01
      • 1970-01-01
      • 2015-07-10
      • 2010-11-02
      相关资源
      最近更新 更多