How it can use datatables/editos with the Django rest framework properly?

How it can use datatables/editos with the Django rest framework properly?

KontiSysKontiSys Posts: 1Questions: 1Answers: 0

Could someone show me an example code of how I can accomplish this?
We have bought the editor, but there is no example with the Django rest framework.
My concern is the editor provided some data like this:
{"data": [
{
/Actual data/
}],
"action": "create"
}

But Django expects only this, with different Methods (DefaultRouter)
{
/Actual data/
}
My first idea was to manipulate by "to_internal_value", but I am not sure it is the best way.
Could someone please help me?

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    Hi,

    Thanks for your message. We don't have a Django server-side library I'm afraid, but I can certainly help with the manipulation of the data that the client-side is sending to the server into something that your framework is expecting.

    To do that, we use the preSubmit event - e.g.:

      editor.on('preSubmit', function (d) {
        var keys = Object.keys(d.data);
        $.extend(d, d.data[keys[0]]); // Flatten field object
        d.id = keys[0];
        delete d.data;
      });
    

    What that will do is to take the first row being edited (to make sure to limit the UI to single row editing - use the editSingle button type) and put its parameters and values at the top level of the object. Just make sure you don't use the field name action (as Editor uses that to tell the server-side what it is doing; create, edit or remove).

    Equally, for the returned JSON, you may need to use postSubmit to modify the data being returned from the server into something more like what Editor is expecting.

    Allan

  • kthorngrenkthorngren Posts: 21,330Questions: 26Answers: 4,951

    I haven't looked at these in detail but here is a Django Datatables Editor framework you can try. This link contains a tutorial of creating your own.

    Kevin

Sign In or Register to comment.