Report with dynamic columns and labels

Hi!
There is a report in our application.
Report’s columns are dynamically selected from the specified fields of the specified database tables.
Description of the dynamic fields (columns) in our model:

attr1 = fields.Char('Attr1')
attr2 = fields.Char('Attr2')
attr3 = fields.Char('Attr3')
...

In the table_query() selection is generated dynamically:

tableA.join(tableB, ...)
.join(tableC, ...)
...
.select(
tableA.field1.as_('attr1'),
tableB.field5.as_('attr2'),
tableC.field10.as_('attr3'),
...
)

We would like column labels ‘Attr1’, ‘Attr2’, ‘Attr3’ to change into ‘Field1’, ‘Field5’, ‘Field10’ respectively
(both in query results tree view and in ods-report).
We managed to do this in ods-report, but failed in tree view.
We tried to change ‘string’ attributes of these columns in view_attribute() method, but this does not work.
Is it doable in Tryton?

Thank you.

Normally it should work. But I see that in tryton it is the field attrs that is used instead of the XML attrs. In sao, it seems to be OK.
Could you confirm and then report it as an issue.

In fact, users can generate many reports and see results in one tree view.
They choose the type of report in the context of the tree view.
Each report has its own set of columns and labels.
We would like to change the column labels (‘string’ attributes)
in the same tree view after choosing the required report.

Yes, setting string attribute in view_attribute should work if it is based on context, except for Issue 8123: String XML attribute on tree view are not used - Tryton issue tracker but in sao it should work.

We have performed the following test.
Our model for tree view:

attr1 = fields.Char('Attr1')
attr2 = fields.Char('Attr2')
attr3 = fields.Char('Attr3')
...

Our model for tree view context:

checkbox1 = fields.Boolean('Checkbox1')
label1 = fields.Char('Label1')
...

Our view_attributes() method:

@classmethod
def view_attributes(cls):
    return [
        ('/tree/field[@name="attr1"]', 'tree_invisible', Eval('checkbox1', False)),
        ('/tree/field[@name="attr1"]', 'string', Eval('label1', 'Attr1')),
        ]

The column attr1 is hidden if checkbox1 == True and it is shown if checkbox1 == False.
But the label of the column attr1 does not depend on the field label1 and it is always equal to
{"__class__": "Eval", "v": "label1", "d": "Attr1"}.
What are we doing wrong?

P.S. We use “usual” Tryton client (not web client).

You can not have PYSON statement for the string. The string must always be static.
You can change the string value in view_attributes conditionally depending on the context.

For example:

@classmethod
 def view_attribute(cls):
    attributes = [
        ('/tree/field[@name="attr1"]', 'tree_invisible', Eval('checkbox1', False)),
        ]
    if Transaction().context.get('label1'):
        attributes += [
             ('/tree/field[@name="attr1"]', 'string', "Attr1"),
        ]
    return attributes

This only works when you first call Tryton client.
After changing the field label1 in the context, the label of the column attr1 does not change.
It does not change even if we add the call of view_attributes() to our table_query() method.

I do not understand, you always have to use the client to see the view.

If you are using my example, changing the value of the label1 key does nothing because the test is about the presence or not of the key.

OK. Can we change the label of the column attr1 to the value in the field label1 after changing this field in the context?

No, you can not because the value of a field does not exist in the absolute. It only exists for a record.
So as I said with my example you can change the string of a label but only for another static value.

It’s a pity. It seems it would be useful for developers to change labels dynamically.

It can be changed dynamically but only base on thing that makes sense. A view show many records so it can not depend on the value of a single record.

This makes sense in our case. :slightly_smiling_face: We implemented a report designer in our application. Users can create their own reports with the specified fields (columns) from the specified database tables.
So we need to change the column labels dynamically.
We managed to do this in ods-reports, but failed in tree view.

In that case you would probably have to generate and store a view based on your report data, and define an action that would use those views.

We just want to change column labels in tree view dynamically. We have already learned how to change report columns and even column labels in ods reports dynamically.

It is not supported. Once the view is displayed in a tab, the label are static for this tab.
You must open the view using an action with a context that define which labels to display for the all life of the tab.

Do you mean that the view and its context can be generated dynamically?
Could you give a simple example, please?

Yes, you can by overriding fields_view_get. There is an example on the babi module from nantic

Lately we have been busy with another task.
But finally our problem with dynamic columns was solved.
Many thanks to everyone for the valuable advice.
Special thanks to @pokoli for the good example.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.