tastypie - 如何设置权限以允许所有 PUT 和 POST

tastypie - how to set authorization to allow PUT and POST for all

我正处于开发小型 django / tastypie 的早期阶段 api。

如何设置 tastypie 授权以允许所有人对资源执行 PUT 和 POST?

这是我的模型:

class Workload(models.Model):
    name = models.CharField(max_length=120)
    description = models.TextField()
    image = models.CharField(max_length=120)
    flavor = models.CharField(max_length=120)

    class Meta:
        ordering = ["name", ]

这是我的资源:

class WorkloadResource(ModelResource):
    def obj_create(self, bundle, request=None, **kwargs):
        return super(WorkloadResource, self).obj_create(bundle, request)

    def obj_update(self, bundle, request=None, **kwargs):

        workload = Workload.objects.get(id=kwargs.get("pk"))
        workload.description = bundle.data.get("description")
        workload.name = bundle.data.get("name")
        workload.image = bundle.data.get("image")
        workload.flavor = bundle.data.get("flavor")
        workload.save()

        def determine_format(self, request):
            return 'application/json'

    class Meta:
        queryset = Workload.objects.all()
        authorization= Authorization()

如果您指的是所有资源,您可以创建一个基础资源 class,您从以下资源扩展:

class BaseModelResource(ModelResource):
    class Meta:
        allowed_methods = ['put', 'post']

class WorkloadResource(BaseModelResource):
    pass