How to change the context of an existing report?

I would like to add a value to the context of the sales report:

[...]
from trytond.modules.sale import SaleReport

class MySaleReport(SaleReport):
    __name__ = 'sale.my_sale'

    @classmethod
    def get_context(cls, records, data):
        context = super(MySaleReport, cls).get_context(records, data)
        context['totals'] = 'Test totals'
        return context
[...]

But I have the following error:

ImportError: cannot import name 'SaleReport' from 'trytond.modules.sale' (/xxx/xxx/trytond/trytond/modules/sale/__init__.py)

by looking more closely at the definition of the CompanyReport report I realize that in the __init__ I have:

[...]
from .company import CompanyReport

__all__ = ['register', 'CompanyReport']
[...]

Is this why I cannot import the SaleReport?
How to overload the context of a report?

Regards

You have to override the context of a class with the same name as the internal name of the report.

You should use sale.sale as class name in order to extend the sale report.

Ok, i have corrected my classe name …

class MySaleReport(SaleReport):
    __name__ = 'sale.sale'

… but i still have the same error :frowning:

If the goal is to extend the existing report, you must not inherit from the class but register in the report pool just a class inheriting from PoolMeta. It is exactly the same as for the models.

@ced’s reply above is the right answer, but the reason for the error is because you are trying to import it from the package instead of the module.

Instead try:

from trytond.modules.sale.sale import SaleReport

obviously, thank you.

it works :slight_smile: a syntax error in the name of the module made me think that it was necessary to do differently … sorry for the noise

1 Like