how to return nested json - ajax option

how to return nested json - ajax option

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

Hello everyone,

In my case, the data with id between 1 and 2000.

I have json in below shown format:

"Samples": [
    {
      "id": "15",
      "hospital_date": "01-01-2021",
      "source_sample": {
        "type": "X",
        "suffix": "G",
        "description": "result"
      },
      "projects": {
        "name": "Oslo",
        "prefix": "S",
        "description": "result"
      }
    },

Models.py

class Samples(models.Model):
     hospital_date = models.DateField()
class SourceSample(models.Model):
     type = models.CharField(max_length=128)
     suffix = models.CharField(max_length=128)
     description = models.CharField(max_length=256)
     source_sample = models.ForeignKey(Samples, on_delete=models.CASCADE)
class Projects(models.Model):
     name = models.CharField(max_length=128)
     prefix = models.CharField(max_length=128)
     description = models.CharField(max_length=256)
     projects = models.ForeignKey(Samples, on_delete=models.CASCADE)

Base.html

var table = $('#example').DataTable({

         "ajax": {
         url: "/json",
         type: "GET"
        },
        "columns": [ ]

My question is that json format is correct when we consider foreign key relationship? If it is correct, how can I show the json data inside the table line by line with Ajax?

Thank you for support.

This question has an accepted answers - jump to answer

Answers

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

    This example shows how to display nested objects.

    Kevin

Sign In or Register to comment.