# How to create a table which is connected with Model.

**URL:** <https://forum.djangoproject.com/t/how-to-create-a-table-which-is-connected-with-model/8963>\
**Category:** Using Django\
**Created:** [July 27, 2021, 3:11am UTC](https://forum.djangoproject.com/t/how-to-create-a-table-which-is-connected-with-model/8963 "2021-07-27T03:11:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shrestha7](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/shrestha7/32/5287_2.png) [@Shrestha7](https://forum.djangoproject.com/u/Shrestha7)\
**Post date:** [July 27, 2021, 3:11am UTC](https://forum.djangoproject.com/t/how-to-create-a-table-which-is-connected-with-model/8963/1 "2021-07-27T03:11:23Z")

</div>

I want to create a table with the input fields included and doing some calculations inside the table taking out sum of columns and multiply columns. Can anyone guide me on how can I get that result?  
Something like this:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/2X/e/ef0117b2325d169aba48a6d4062c744afa89a7a0.png)

---

<div class="post-metadata">

**Author:** ![megaleunam](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/megaleunam/32/5289_2.png) [@megaleunam](https://forum.djangoproject.com/u/megaleunam)\
**Post date:** [July 27, 2021, 3:58am UTC](https://forum.djangoproject.com/t/how-to-create-a-table-which-is-connected-with-model/8963/2 "2021-07-27T03:58:53Z")

</div>

hello, you can calculate data by writing the save method of your model and store the result in another field or model. but if you want it to be in real time the calculation has to be with javascript or jquery.

I hope this helps you:

> **[How to override the save method in your Django models](https://www.sankalpjonna.com/learn-django/how-to-override-the-save-method-in-your-django-models)**
>
> A tutorial on how to best perform actions before or after a Django model is saved to the database

---

<div class="post-metadata">

**Author:** ![michjnich](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/michjnich/32/2424_2.png) [@michjnich](https://forum.djangoproject.com/u/michjnich)\
**Post date:** [July 27, 2021, 9:13am UTC](https://forum.djangoproject.com/t/how-to-create-a-table-which-is-connected-with-model/8963/3 "2021-07-27T09:13:02Z")

</div>

The table structure you will have to create in your template or in the front end.

If you’re using “pure” Django (and not react, Vue etc as the front end), then what you probably want to look at is formsets.

[https://docs.djangoproject.com/en/3.2/topics/forms/formsets/](https://docs.djangoproject.com/en/3.2/topics/forms/formsets/)

In particular for your case, you will likely want `inlineformset_factory()`

[https://docs.djangoproject.com/en/3.2/ref/forms/models/#inlineformset-factory](https://docs.djangoproject.com/en/3.2/ref/forms/models/#inlineformset-factory)

As far as the totals are concerned, when you change the data you will have to update this with javascript - either that or resubmit the form every time and refresh the page, which is perhaps not the most performant option.

---

<div class="post-metadata">

**Author:** ![Shrestha7](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/shrestha7/32/5287_2.png) [@Shrestha7](https://forum.djangoproject.com/u/Shrestha7)\
**Post date:** [July 29, 2021, 9:13am UTC](https://forum.djangoproject.com/t/how-to-create-a-table-which-is-connected-with-model/8963/4 "2021-07-29T09:13:37Z")

</div>

Thanks for the reply. Now I have another question:  
How am I suppose to use forms when I have already TemplateView and want that forms to be inside that same TemplateView?

---

<div class="post-metadata">

**Author:** ![michjnich](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/michjnich/32/2424_2.png) [@michjnich](https://forum.djangoproject.com/u/michjnich)\
**Post date:** [August 1, 2021, 10:03am UTC](https://forum.djangoproject.com/t/how-to-create-a-table-which-is-connected-with-model/8963/5 "2021-08-01T10:03:12Z")

</div>

You just put a form into your template. Since you’re not using any kind of model view, you can’t handle it automatically, so you’ll need to populate the context with the data you want, but this seems more of an html question than a django specific one … You’ll of course want a `post` method on the view itself to handle the form.
