Displaying a "group by" list as a model field

Hello, I’m experiencing diffculties on the development of my custom module, and I’m very new to Tryton dev, so I’m asking for your help, your knowledge and your experience on this framework.
Here is a screenshot of my module right now :

My module implements a new model called “SE Data”, which goal is to make statistics on various data of the ERP, on a chosen period set in the “Start of the Period” and “End of the Period” editable fieds. Only the three fields at the top are editable, the other fields are calculated using data from the database, so they are read-only.

The list you can see is a list of purchases (registered in the database) which purchase date is included into the period given to the “SE Data” instance. The boolean fields are fields I added to the Purchase and Party modules, to detect if a purchase is “sustainable/responsible”. Below this list, I calculated statistics on the rate of “responsible” purchases and other statistics.

My problem is that I would like to display another list than this list of purchases : instead, I would like to display a list of the suppliers (basically party instances) with the same boolean fields, and the total of the amounts of all the purchases made to this supplier in the given period. Basically, I would like to display the same list, group by party, with the sum of the amounts of the purchases corresponding to the party.

I could not find any element in the documentation that would simply make a “group by” operation on a list of instances of a model. So I thought about 2 solutions, and I did not manage to implement neither of them. These solutions are :

  • Creating a new model and generating new instances of this model triggering the creation of a new SE Data instance, but I did not find how to trigger events and create a new instance in the code
  • Using this example suggested by ced, but the XML implementation of the HoursEmployee model makes me think it won’t be possible to display such list as a field of another model, neither to choose as a context the fields of my SE-Data model

I’m waiting for your suggestions on new ideas to implement it, or some elements to help me implementing the solutions I’ve thought about. You can ask me to give details on how I tried to implement the solutions above if it can help. Also, if you think that the general implementation of my module does not fit with Tryton spirit, feel free to suggest me other ways to implement this feature more respectfully

You should use a context model for that. See for example the “General Ledger”.

You must define a new model which is not stored in the database but from a table query which perform the required grouping.

Hello Cédric !
I thought about this solution, but if I do so, will I be able to display the calculated data, visible in my first screenshot, in the blank space under the tree view, visible in the following screenshot ?

And if not, is it possible to use such model as a field in another model, so that I can display my list, then my statistics in the same window ?

Thanks for your answers.

No you can not because there is no form with a record.
Maybe it is possible on the context model to add some function fields which compute those values (I never tried).

Otherwise we may try to have a mechanism like described in Issue 3703: Show sum of all loaded selected records - Tryton issue tracker but more generic to support other operation than sum.

Thank you so much for your fast answers.

Can I ask your opinion on the other solution I thought about :

In the documentation, I found a brief description of the trigger functions, I thought I could use it to make actions after detecting the creation of an instance of a SE Data, but I did not see many usage of this in the official Tryton modules. Is it because it does not fit Tryton development rules ? Moreover, I could not really find functions to create instances of a model from the code of a module.

Can you tell me if this solution can be implemented in Tryton ? And if it can, please can you show me wich functions allow to do so ?

It is not used because the trigger mechanism is mainly for customization by the end user. When we need such behavior we directly extend the CRWD functions to add the needed code.

I’m not sure to understand the question. But you can retrieve the model from the pool Pool().get('<model name>) and then you can either instantiate a record and save it record = Model(); record.save(); or you can call directly the create method Model.create([{…}]).

You can create new record at the creation of another. But usually we do find that it complicates the design and the code (usually there are problem to maintain both records synchronized). As such data does not add new information to the system, we usually prefer to use Model based on table query to construct the data layer that we want.

Thank you very much for your answers !
Thanks to all these indications, I think I am going to implement the solution with the model based on a table query, and display my statistic data in another view. I hope that later I could implement a button to switch from the tree view of my suppliers to a form view with my statistic fields, to simplify the access from one to the other.

I made a quick test and it works. So you could add Function fields using as getter an on_change_with with the proper depends decorator.
But of course the computation can not depend on the record displayed nor selected.

Ok, thank you for your test !
I wanted to ask you again this question, because I am not sure you answered to it :

By “such model” I mean the model based on the table_query, is it possible to use this model as a field of another model and display its tree view inside the form view of the parent model ?

I guess you want to have a One2Many field targeting a table query model. This can be done as long as you have stable ids and have a Many2One (or Reference) field filled with the id of the source model.

This is exactly what I mean, I will try and give my feedback later !