# Create tablequery with number of columns variable

**URL:** https://discuss.tryton.org/t/create-tablequery-with-number-of-columns-variable/4758
**Category:** Developer
**Created:** [November 11, 2021, 2:33pm UTC](https://discuss.tryton.org/t/create-tablequery-with-number-of-columns-variable/4758 "2021-11-11T14:33:39Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![andres53016](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/andres53016/32/406_2.png) [@andres53016](https://discuss.tryton.org/u/andres53016)
#### Post date: [November 11, 2021, 2:33pm UTC](https://discuss.tryton.org/t/create-tablequery-with-number-of-columns-variable/4758/1 "2021-11-11T14:33:39Z")

</div>

Hello everyone, I have a problem that due to my inexperience in tryton I don’t know how to solve it and I didn’t find a similar module. I have to plan jobs. And it is required to be loaded as follows.

` job; required_quantity; planned_quantity;month_1; month_2; .....`

I created the following table to store the data:  
`job; month_number; quantity`

I can think of two solutions:

1. Have 12 columns of months and by means of a field pass the months offset value to it.The problem I have is that I don’t know how to pass this displacement value to my tablequery (I have implemented this, both the obtaining of the data by tablequery, as well as the write method to save the data).
2. To have a tablequery with variable columns. The problem here I do not know if it is possible from tryton.

Thanks in advance.

---

<div class="post-metadata">

### Author: ![pokoli](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/pokoli/32/22_2.png) [@pokoli](https://discuss.tryton.org/u/pokoli)
#### Post date: [November 11, 2021, 2:49pm UTC](https://discuss.tryton.org/t/create-tablequery-with-number-of-columns-variable/4758/2 "2021-11-11T14:49:59Z")

</div>

You need to create a model with a column for each month.

Then using the table query you can use the [filter attribute of the aggregate](https://hg.tryton.org/python-sql/file/tip/sql/aggregate.py#l14) to filter the agregate for each month. We have an [example on the spanish tax reporting](https://hg.tryton.org/modules/account_es/file/65d9c5670a4f/reporting_tax.py#l479)

---

<div class="post-metadata">

### Author: ![andres53016](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/andres53016/32/406_2.png) [@andres53016](https://discuss.tryton.org/u/andres53016)
#### Post date: [November 11, 2021, 2:54pm UTC](https://discuss.tryton.org/t/create-tablequery-with-number-of-columns-variable/4758/3 "2021-11-11T14:54:55Z")

</div>

Thank you very much for your response. I will check what you sent me. The problem is that the number of months depends on the project, it can be from 5 months to 36 months for example, there is no limit of duration.

---

<div class="post-metadata">

### Author: ![andres53016](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/andres53016/32/406_2.png) [@andres53016](https://discuss.tryton.org/u/andres53016)
#### Post date: [November 11, 2021, 5:14pm UTC](https://discuss.tryton.org/t/create-tablequery-with-number-of-columns-variable/4758/4 "2021-11-11T17:14:01Z")

</div>

> [@pokoli](#):
>
> You need to create a model with a column for each month.

This part I don’t understand.

> [@pokoli](#):
>
> Then using the table query you can use the [filter attribute of the aggregate](https://hg.tryton.org/python-sql/file/tip/sql/aggregate.py#l14) to filter the agregate for each month.

This rocks! It improved the performance a lot, I had done it with 12 joins.

I saw that with context\_model I can filter the results, so in this case I can choose from which month I will start showing the results, but I saw that it applies to act\_windows, I wanted to know if I can do the same for a view, since I want to show these results as a One2Many field, from another view.  
I add a screenshot of how I want the interface to look like

 ![Screenshot_20211111_141002](https://discuss-cdn.tryton.org/uploads/default/original/2X/0/01f4b9bf39b618118692e7a3a584f0f34dc0d8f7.png)

---

<div class="post-metadata">

### Author: ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)
#### Post date: [November 11, 2021, 5:37pm UTC](https://discuss.tryton.org/t/create-tablequery-with-number-of-columns-variable/4758/5 "2021-11-11T17:37:14Z")

</div>

With Tryton you can not have arbitrary/dynamic columns (for now). So I think month columns should be a list of month stored/displayed in a One2Many.

---

<div class="post-metadata">

### Author: ![andres53016](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/andres53016/32/406_2.png) [@andres53016](https://discuss.tryton.org/u/andres53016)
#### Post date: [November 11, 2021, 5:48pm UTC](https://discuss.tryton.org/t/create-tablequery-with-number-of-columns-variable/4758/6 "2021-11-11T17:48:12Z")

</div>

Thanks for the answer Ced. The problem is that the job list is also dynamic and the name is important, that’s why I chose to put the jobs as rows.

---

<div class="post-metadata">

### Author: ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)
#### Post date: [November 11, 2021, 5:50pm UTC](https://discuss.tryton.org/t/create-tablequery-with-number-of-columns-variable/4758/7 "2021-11-11T17:50:22Z")

</div>

But you can have a One2Many in a One2Many. But you can just not display them in a list.

---

<div class="post-metadata">

### Author: ![andres53016](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/andres53016/32/406_2.png) [@andres53016](https://discuss.tryton.org/u/andres53016)
#### Post date: [November 11, 2021, 5:59pm UTC](https://discuss.tryton.org/t/create-tablequery-with-number-of-columns-variable/4758/8 "2021-11-11T17:59:15Z")

</div>

I don’t understand what you want to explain.

As a context, the idea of the module is to see all the tasks to be done and to load which ones are going to be done and how many of them are going to be done in a specific month. It is proposed in this way so that visually it is easy to plan the tasks for the user.

---

<div class="post-metadata">

### Author: ![andres53016](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/andres53016/32/406_2.png) [@andres53016](https://discuss.tryton.org/u/andres53016)
#### Post date: [November 11, 2021, 6:05pm UTC](https://discuss.tryton.org/t/create-tablequery-with-number-of-columns-variable/4758/9 "2021-11-11T18:05:07Z")

</div>

There I understood, I would place an O2M for quantities per month and the only columns I would have would be:

` job; required_quantity; planned_quantity;quantities_per_month(O2M).`

But I would have the problem that it would not be easy to contrast with the other tasks.

---

<div class="post-metadata">

### Author: ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)
#### Post date: [November 11, 2021, 10:30pm UTC](https://discuss.tryton.org/t/create-tablequery-with-number-of-columns-variable/4758/10 "2021-11-11T22:30:16Z")

</div>

Another way would be to have just fixed number of columns and compute them relatively to today. Like current month, next month etc.

---

<div class="post-metadata">

### Author: ![andres53016](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/andres53016/32/406_2.png) [@andres53016](https://discuss.tryton.org/u/andres53016)
#### Post date: [November 11, 2021, 10:43pm UTC](https://discuss.tryton.org/t/create-tablequery-with-number-of-columns-variable/4758/11 "2021-11-11T22:43:16Z")

</div>

I’m trying to add an offset of where the first month starts as shown in the figure, but I can’t get the start\_month field through the context.

So I’m looking at making it a window to use the context\_model. It is not what I want, but I can’t solve it the other way.
