django-tastypie PATCH给我“ 400(错误请求)”

提问

我正在Apache上运行Django站点,该站点以Nginx实例为前端,以服务我的静态媒体.

我通过django-tastypie将API公开给我需要在其上修补字段的模型.当我执行本地测试(通过django runserver)时,一切正常.但是,在实时服务器上,我返回了“ 400(错误请求)”.

我读过一些地方说Nginx不支持PATCH?那正确吗?有一个好的解决方法吗?难道我做错了什么?

我只发送要通过postData更新的字段.

jQuery代码:

$.ajax({url: '...',
    type: 'PATCH',
    accepts: 'application/json',
    contentType: 'application/json',
    dataType: 'json',
    data: postData,
    processData: false,
    success: function() {
        // Success Code!
    },
    error: function() {
        // Error Code!
    }
});

美味资源:

class ReceivedMessageResource(ModelResource):
    """
    """
    campaign = fields.ForeignKey(CampaignResource, 'campaign')
    campaign_name = fields.CharField(readonly=True)
    campaign_id = fields.IntegerField(readonly=True)
    message_type = fields.CharField(readonly=True)
    display_date = fields.CharField(readonly=True)
    attachments = fields.ToManyField('apps.campaign.api.AttachmentResource',
                                     'attachment_set',
                                     related_name='message',
                                     full=True)

    class Meta:
        queryset = ReceivedMessage.objects.all()
        resource_name = 'message'
        filtering = {'id': ALL,
                     'campaign': ALL_WITH_RELATIONS}
        excludes = ['reason', 'provider', 'loyalty_profile', 'original_message', 'date_received']
        allowed_methods = ['get', 'post', 'put', 'delete', 'patch']
        paginator_class = ReceivedMessagesPaginator
        authentication = ApiKeyAuthentication()
        authorization = DjangoAuthorization()

如何排序的任何方向将不胜感激:)

最佳答案

如果您正在使用最新版本的TastyPie(自8月5日起,是GitHub存储库中的版本),则可以按照from the documentation的说明进行操作:

Using PUT/DELETE/PATCH In Unsupported Places

Some places, like in certain browsers or hosts, don’t allow the PUT/DELETE/PATCH methods. In these environments, you can simulate those kinds of requests by providing an X-HTTP-Method-Override header. For example, to send a PATCH request over POST, you’d send a request like:

curl --dump-header - -H "Content-Type: application/json" -H "X-HTTP-Method-Override: PATCH" -X POST --data '{"title": "I Visited Grandma Today"}' http://localhost:8000/api/v1/entry/1/

因此,如果您的主机不支持此方法,请添加X-HTTP-Method-Override标头以及您要执行的方法的名称.