How to return nested json - ajax option

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? https://datatables.net/

Thank you for support.

It is a valid format for that relationship under a specific situation - when you’re only retrieving one Projects for your Samples. However, since Projects is in a One-To-Many relationship with Samples, it is invalid in the general case.

It depends upon exactly what you want the output to look like.

Since you’re only retrieving one Projects for your Samples, do you want this Projects to appear on the same row with the Samples data?

If that’s the case, then you don’t want to format your JSON this way - you’d be a lot better off just adding those data elements to the parent row.

Or are you looking to show Projects as a datatables child row? In this case you will need to write some JavaScript to build those rows from the retrieved data.

In either case, these are datatables questions and not really Django questions. You may be able to get more detailed and specific assistance in a datatables-related forum.

Thank you for the feedback. I really appreciate it.

Yes, I want Projects and SouceSample to appear on the same row with Samples data.
I want One-To-Many relationship with Samples.

But I dont know how to fix json in an efficient way. (I’ll ask Ajax part to datatable forum)

ID:Samples data id.

ID. Project_name Prefix Descrip Type Suffix Descrip Hospital_date
15 Oslo S result X G result 01-01-2021
-------- -------------------- ---------- ------------ -------- -------- ------------- ---------------------
16 Kiev S result X G result 03-03-2021
-------- -------------------- ---------- ------------ -------- -------- ------------- ---------------------
17 Kiev S result X G result 05-05-2021
-------- -------------------- ---------- ------------ -------- -------- ------------- ---------------------

Your diagram does not match your model. Your diagram implies that multiple Samples can all be related to the same Projects.
However, your model is defined in the reverse. You’ve got it defined such that each Samples can be related to multiple Projects.
(In a One-to-Many relationship, the foreign key exists on the “many” side of that relationship.)

So which is accurate here?

Hello, I tried to fix my mistake. I hope, this time implementations of models are correct.

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

JSON

"Data": {
    "samples": {
      "id": "1",
      "hospital_date": "01-01-2021",
      "source_sample": {
        "type": "T",
        "suffix": "G",
        "description": "result"
      },
      "project": {
        "project_id": "1",
        "name": "Valencia",
        "prefix": "S",
        "description": "result"
      },
    }
  }

My question is that json format is correct? If I want to add more project or sourcesample data linked in sample model, what will the final version of the json format look like?

Really thank you for feedbacks.

The best we can say is that it is valid JSON.

Datatables is extremely flexible with what it can accept as JSON data, so if your question is “can datatables work with this JSON structure”, then I’d say yes.

Note that fundamentally, the format of the data that datatables uses doesn’t change between embedding the JSON data in the page and retrieving it via AJAX. You can work on your datatables implementation without using AJAX to make datatables work with your data before you get AJAX into the mix.

1 Like