Datatable multiple models -Foreign Key - Json%Ajax

Hello everyone,

I’m able to use datatable with one class model by getting json data. However, I would like to add another class with foreign key to models.py. Something like this:

class AnotherModel(models.Model):

     description = models.CharField(max_length=256)
     ibvs = models.ForeignKey(Ibv, cascede mode on)

How I can arrange json get_data function and ajax ?

And, I am super confused about json file format & how to pass AnotherModel values to the table.(I’d like to show all models in one table)

MODELS.PY

class Ibv(models.Model):

     code = models.CharField(max_length=256)
     muestra = models.CharField(max_length=256, null=True, choices=MUESTRAS)
     ship = models.CharField(max_length=256)
     num =  models.CharField(max_length=256)
     enc = models.CharField(max_length=256)
     obv = models.CharField(max_length=256)
     result = models.TextField()
     created_at = models.DateField(auto_now=True)

def get_data(self):

         return {

          'id': self.id,
          'code': self.code,
          'muestra': self.muestra,
          'ship': self.ship,
          'num': self.num,
          'enc': self.enc,
          'obv': self.obv,
          'result': self.result,
          'created_at': self.created_at,
        }

VIEWS.PY

def ibv_json(request):

       ibvs = Ibv.objects.all()
       data = [ibv.get_data() for ibv in ibvs]
       response = {'data': data}
       return JsonResponse(response)

BASE.HTML

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

// json to fill the table rows and columns

         "ajax": {
         url: "/json",
         type: "GET"
        },
        "columns": [
             {"data": "id"},
             {"data": "code"},
             {"data": "muestra"},
             {"data": "ship"},
             {"data": "num"},
             {"data": "enc"},
             {"data": "obv"},
              // {"data": "result"},
             {"data": "created_at"},

Thank you for your help in advance.

It’s not clear what exactly you’re looking for.

AnotherModel has a many-to-one relationship with Ibv, so unless you arbitrarily say you’re going to take one specific instance, you might end up with multiple AnotherModel associated with an instance of Ibv.

So what do you want your get_data dict to look like for this?

1 Like

Right now, I’m able to get data from database and show those data at datatable ( https://datatables.net/ ) with simple JSON and Ibv model.

one row sample:
data: { “id”: 1, “code”: cov1, “muestra”: rna, “ship”: ES-1, “num”: num1, “enc”: 1, “obv”: 1, “created_at”: 2021}

However, I want to add two more models associated with it to the existing model (Ibv). This is exactly where I’m stuck. Nested JSON confused me a bit. I want to figure out how to edit the get_data dict to create nested JSON and show these three models in one table (ajax - “columns” part).

PS: Hope I was clear enough. My english is not so good.

class Projects(models.Model):
     prefix = models.CharField(max_length=128)
     description = models.CharField(max_length=256)
     ibvs = models.ForeignKey(Ibv, on_delete=models.CASCADE)
class SourceSample(models.Model):
     suffix = models.CharField(max_length=128)
     description = models.CharField(max_length=256)
     ibvs = models.ForeignKey(Ibv, on_delete=models.CASCADE)

First, a side note: Datatables does not directly support nested tables. You’re going to need to do some work in JavaScript in your datatables definition to get the results you may be looking for.

Now, one way to do this would be to extend your get_data method in your model.

For example, if you want the prefix and description fields for all Projects associated with an instance of Ibv, you could add something like the following to your dict definition within the get_data method:

'projects' :  [(project.prefix, project.description) for project in self.projects_set.all()]

(Note: I’m winging this, there may be an error in there, but hopefully it’s enough to give you an idea.)

1 Like