Tastype在何处限制可由修补程序更新的字段?

2024-09-30 10:30:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个有效的GET/tastype(只读)解决方案。在

我已经允许了PUT/PATCH请求并成功地修补了一个记录。在

不过,我希望将修补程序功能限制为(已经)经过身份验证和授权的用户在适当的modelresources上的某些字段。我仍然希望用户能够获得(看到)所有字段。在

哪里是最好的地方(方法?)为了达到这种限制?在

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


Tags: 文件方法用户程序功能身份验证getput
2条回答

既然您似乎已经拥有了对用户的授权,您应该能够通过添加到ModelResource中的元类来实现这一点。例如,使用djangauthorization(from tastypie docs):

from tastypie.authentication import BasicAuthentication
from tastypie.authorization import DjangoAuthorization
...

class SomeResource(ModelResource):
  ...
  class Meta:
    ...
    authentication = BasicAuthentication()
    authorization = DjangoAuthorization()

此示例将为您提供django.contrib.auth.models.Permission中定义的操作的用户授权。在

我还有来自Tastype Google群组的this。{a3}使用^。以下是Google Groups链接中提供的示例:

^{pr2}$

有点晚了,但也许这会对某人有所帮助。在

我的解决方案是重写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
        )

相关问题 更多 >

    热门问题