[Answer]-Django Rest Framework many related field โ€“ Javascript ajax โ€“ corret format for list of ints

1๐Ÿ‘

โœ…

So I found the answer in this question:

How to send a list of int with jQuery to ASP.net MVC Default Model Binder

The solution is to use the option traditional=true in the ajax call

var categories = [1,2,3];
$.ajax({
    url : "/api/items/id",
    type: 'POST',
    traditional:true,
    headers: {
        'X-HTTP-Method-Override': 'PUT'
    },      
    // Form data
    data : {
      'categories':categories
    }

then the parameters are transfered correctly as a list.
});

๐Ÿ‘คoverlii

Leave a comment