Why is data empty when deleting a table record

Why is data empty when deleting a table record

mgpearce48mgpearce48 Posts: 23Questions: 6Answers: 0

According the documentation for REST Interface, when deleting a record datatables editor will submit data as follows:

data[row_3][DT_RowId]=row_3
data[row_3][first_name]=Ashton
data[row_3][last_name]=Cox
data[row_3][position]=Junior Technical Author
data[row_3][email]=a.cox%40datatables.net
data[row_3][office]=San Francisco
data[row_3][extn]=1562
data[row_3][age]=66
data[row_3][salary]=86000
data[row_3][start_date]=2009-01-12
action=remove

However when I submit the data dictionary for Django REST Framework it is empty:

"data":{}

Here is my javascript:

  <script type="text/javascript">

    var editor;
    $(document).ready(function() {

        editor = new $.fn.dataTable.Editor( {
            table: "#object-table",
            ajax: {
                create: {
                    type: 'POST',
                    url:  "{% url 'facility-list' format='datatables' %}",
                    headers: {'X-CSRFToken': '{{ csrf_token }}'}
                },
                edit: {
                    type: 'PUT',
                    url:  "{% url 'facility-detail' pk=None format='datatables' %}",
                    headers: {'X-CSRFToken': '{{ csrf_token }}'}
                },
                remove: {
                    type: 'DELETE',
                    url:  "{% url 'facility-detail' pk=None format='datatables' %}"
                }
            },

            idSrc:  'bldgcode',

            fields: [
                { label: "Building Code:", name: "bldgcode" },
                { label: "Name:", name: "name" },
                {
                    label: "Category:",
                    name: "category",
                    type: "select",
                    options: []
                },
                {
                    label: "Location:",
                    name: "location",
                    type: "select",
                    options: []
                }
            ],

            i18n: {
                create: {
                    button: "Add",
                    title:  "Add new facility",
                    submit: "Add"
                },
                edit: {
                    button: "Edit",
                    title:  "Edit facility details",
                    submit: "Update"
                },
                remove: {
                    button: "Delete",
                    title:  "Delete facility",
                    submit: "Delete",
                    confirm: {
                        1: "Are you sure you want to delete the selected facility?"
                    }
                }
            }

        } );

        $.fn.dataTable.Buttons.defaults.dom.button.className = 'btn btn-sm btn-primary';
        var table = $('#object-table').DataTable( {

            pageLength: 10,
            order: [[ 0, 'asc' ]],
            processing: true,
            serverSide: true,
            dom: "lBfrtip",
            ajax: "{% url 'facility-list' format='datatables' %}",
            select: 'single',

            columns: [
                { data: "bldgcode" },
                { data: "name" },
                { data: "category_label" },
                { data: "location_name" }
            ],

            buttons: [
                { extend: "create", editor: editor},
                { extend: "edit",   editor: editor},
                { extend: "remove", editor: editor}
            ]

        });

        table.buttons().container()
            .appendTo($('.col-md-6:eq(0)', table.table().container()));

        editor.field('category').input().addClass('form-control');
        editor.field('location').input().addClass('form-control');

    });

  </script>

Any assistance would be greatly appreciated.
Thanks,
mgpearce48

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,167Questions: 26Answers: 4,921
    Answer ✓

    Have you verified what is in the XHR request headers using the browser's Developer Tools > Network tab?

    When you say the "data":{} is submitted are you saying that is what your Django script receives from the client or what it sends back?

    Kevin

  • mgpearce48mgpearce48 Posts: 23Questions: 6Answers: 0

    From the browser using the Developer Tools > Network tab > Body tab:

    {"data":{},"recordsFiltered":0,"recordsTotal":0,"draw":1,"options":{"location":[{"label":"Adelaide","value":3123},{"label":"Alice Springs","value":3133},{"label":"Auckland","value":3143},{"label":"Australia","value":3124},{"label":"Avalon","value":3137},{"label":"Bangkok","value":3142},{"label":"Beijing","value":3135},{"label":"Brisbane","value":3118},{"label":"Broome","value":3145},{"label":"Cairns","value":3127},{"label":"Canberra","value":3122},{"label":"Coffs Harbour","value":3150},{"label":"Coolangatta","value":3146},{"label":"Darwin","value":3134},{"label":"Devonport","value":3120},{"label":"Dubai","value":3141},{"label":"Emerald","value":3136},{"label":"Haneda Airport","value":3148},{"label":"Hobart","value":3126},{"label":"Hong Kong","value":3130},{"label":"Jakarta","value":3116},{"label":"Johannesburg","value":3131},{"label":"Karatha","value":3128},{"label":"London","value":3115},{"label":"Los Angeles","value":3147},{"label":"Melbourne","value":3119},{"label":"Narita","value":3151},{"label":"New Zealand","value":3132},{"label":"Newcastle","value":3121},{"label":"Perth","value":3125},{"label":"Port Lincoln","value":3140},{"label":"Port Moresby","value":3153},{"label":"Santiago","value":3139},{"label":"Shanghai","value":3149},{"label":"Singapore","value":3138},{"label":"Sydney","value":3117},{"label":"Tamworth","value":3144},{"label":"testing","value":3155},{"label":"Tokyo","value":3152},{"label":"Townsville","value":3129}],"category":[{"label":"Terminal","value":"TERMINAL"},{"label":"Hangar","value":"HANGAR"},{"label":"Office","value":"OFFICE"},{"label":"Freight","value":"FREIGHT"},{"label":"Engineering","value":"ENGINEERING"},{"label":"Lounge","value":"LOUNGE"},{"label":"Catering","value":"CATERING"},{"label":"Miscellaneous","value":"MISCELLANEOUS"}]}}
    

    however I note from Developer Tools > Network tab > Parameters tab:

       action: remove
       data%5B1002%5D%5Bbldgcode%5D: 1002
       data%5B1002%5D%5Bcategory_label%5D: Terminal
       data%5B1002%5D%5Blocation_name%5D: Adelaide
       data%5B1002%5D%5Bname%5D: two
    

    I'm not sure which one I'm supposed to be looking at?
    Thanks,
    mgpearce48

  • mgpearce48mgpearce48 Posts: 23Questions: 6Answers: 0

    OK got it sorted. I was looking at the response instead of the request, and was able to retrieve the data via Django request.query_params instead of request.data.
    Many thanks for your help.
    Regards,
    mgpearce48

This discussion has been closed.