tastypie - 在哪里限制可能由PATCH更新的字段?

Ait*_*tch 3 python django patch tastypie

我有一个工作的GET/tastypie(只读)解决方案.

我已经允许PUT/PATCH请求并成功修补记录.

但是,我想将PATCH功能仅限于适当的模型资源上的某些字段,用于(已经)经过身份验证和授权的用户.我仍然希望用户能够获取(参见)所有字段.

实现这种限制的最佳位置(方法?)在哪里?

文档:https://django-tastypie.readthedocs.org/en/latest/interacting.html? highlight = patch#partially -updating-an-existing-resource- patch

Mar*_*cci 13

有点晚了但也许这会对某人有所帮助.

我的解决方案是覆盖update_in_place并检查传递的数据.

from tastypie.resources import ModelResource
from tastypie.exceptions import BadRequest


class MyResource(ModelResource):
    class Meta:
        ...
        allowed_update_fields = ['field1', 'field2']

    def update_in_place(self, request, original_bundle, new_data):
        if set(new_data.keys()) - set(self._meta.allowed_update_fields):
            raise BadRequest(
                'Only update on %s allowed' % ', '.join(
                    self._meta.allowed_update_fields
                )
            )

        return super(MyResource, self).update_in_place(
            request, original_bundle, new_data
        )
Run Code Online (Sandbox Code Playgroud)