json.dumps - table initialisation

json.dumps - table initialisation

goktuginal41@gmail.comgoktuginal41@gmail.com Posts: 57Questions: 22Answers: 0

Hello everyone,

I return the data I have as follows. But I didn't know how to initialize the table. I will be glad if you help. Thanks in advance.

The data returned in orjson type:

http://127.0.0.1:8000/api/App/home
[{"name":"Knock Knock","owner":{"id":1,"birth_year":100,"full_name":"Cardinal, IT"}}]

@router.get("/home")
def home(request):
    musics = Music.objects.all()
    data = [MusicOut.from_orm(i).dict() for i in musics]
    data = orjson.dumps(data)

    return HttpResponse(data, content_type="application/json")
$('#example').DataTable( {
     "ajax": {
            url: "/json",
            type: "GET"
          },
    columns: [
        { data: 'name' },
        { data: 'owner' },
    ]
} );

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,299Questions: 26Answers: 4,945
    Answer ✓

    Datatables looks for the row data to be in the data object. Your data is not in the data object. You can either change the JSON response to include the data object or use ajax.dataSrc, dataSrc: '' in your case, to point to the data. Take a look at the Ajax docs for details.

    Do you want the owner object to be displayed in separate columns? If so this nested objects example will show how.

    Kevin

Sign In or Register to comment.