Skip to main content
 首页 » 编程设计

python之为什么需要在 Tastypie 的 allowed_methods 列表和详细的 allowed_methods 中添加 'POST' 方法

2025年05月04日9bhlsheji

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”工作正常......!

请您参考如下方法:

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

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

因此,注释 detail_allowed_methods 不会有任何作用。如果您想禁用所有方法,请将其值设置为空列表:

detail_allowed_methods = [] 

See Tastypie docs .