Fotis Sk
Fotis Sk

Reputation: 323

Is it possible to make a PUT request on list view, using ModelViewSets?

My goal is to handle a PUT request that contains multiple jsons. In simple words, I just want to be able to update multiple items with 1 PUT request. I tried to override the update() method in my ModeViewSet, but as I saw in my tests, the following request:

(example) response = self.client.put('/collections/', [{'id':1, ...}, {'id':2, ...}])

didn't even go into the update method's code. Instead, it returned a 'method not allowed' error. I looked for this issue and I found out that, by default, drf supports PUT requests only on detail views.

I also found this answer here on stack overflow, but I would prefer to see if it can be done without using a 3rd-party package.

Upvotes: 0

Views: 261

Answers (1)

Linovia
Linovia

Reputation: 20976

Yes to both of your questions.

What you need is to alter the routers so that they map the PUT verb to some ViewSet's method.

You'll have to copy&paste the route definitions from the default router and alter the mapping as - for example:

mapping={
    'get': 'list',
    'post': 'create',
    'put': 'list_update',
},

Upvotes: 1

Related Questions