# Datatable multiple models -Foreign Key - Json%Ajax

**URL:** <https://forum.djangoproject.com/t/datatable-multiple-models-foreign-key-json-ajax/13918>\
**Category:** Templates & Frontend\
**Created:** [May 25, 2022, 2:14pm UTC](https://forum.djangoproject.com/t/datatable-multiple-models-foreign-key-json-ajax/13918 "2022-05-25T14:14:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Goktuginal](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/goktuginal/32/8648_2.png) [@Goktuginal](https://forum.djangoproject.com/u/Goktuginal)\
**Post date:** [May 25, 2022, 2:14pm UTC](https://forum.djangoproject.com/t/datatable-multiple-models-foreign-key-json-ajax/13918/1 "2022-05-25T14:14:53Z")

</div>

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:

```auto
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**

```auto
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**

```auto
def ibv_json(request):

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

```

**BASE.HTML**

```auto
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.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [May 26, 2022, 12:25pm UTC](https://forum.djangoproject.com/t/datatable-multiple-models-foreign-key-json-ajax/13918/2 "2022-05-26T12:25:32Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Goktuginal](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/goktuginal/32/8648_2.png) [@Goktuginal](https://forum.djangoproject.com/u/Goktuginal)\
**Post date:** [May 26, 2022, 3:23pm UTC](https://forum.djangoproject.com/t/datatable-multiple-models-foreign-key-json-ajax/13918/4 "2022-05-26T15:23:31Z")

</div>

Right now, I’m able to get data from database and show those data at datatable ( [https://datatables.net/](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.

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

```

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

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [May 26, 2022, 3:55pm UTC](https://forum.djangoproject.com/t/datatable-multiple-models-foreign-key-json-ajax/13918/5 "2022-05-26T15:55:44Z")

</div>

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:

```auto
'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.)
