Search code examples
djangotastypie

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


I'm in the early stages of developing a small django / tastypie api.

How can I set tastypie authorization to allow all to do PUT and POST on a resource?

Here is my model:

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", ]

And here is my resource:

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()

Solution

  • If you mean for all resources, you can create a base resource class that you extend from:

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