Renders Parent Model Field in Report

I create a report using 2 parent-child models:

  • UserTimesheet
  • UserTimesheetRecord

UserTimesheetRecord has a field Many2One to a model called Project

In database UserTimesheetRecord table is like below, in which the column ‘project’ holds the ID of a project record in project table:
tsheet-record-db

And below is Project data in DB:

based on opportunity tutorial, to renders party module field value, all you need to do is calling it from field placeholder at FODT file like this: opportunity.party.rec_name.

So at FODT or FODS template, I try to renders Project’s name (should be derives from Project table, with field placeholder: record.project.proj_name (at FODT) or relatorio://record.project.proj_name (at FODS).

But both throws error says:

  File "/genshi/template/eval.py", line 397, in undefined
    raise UndefinedError(key, owner=owner)
genshi.template.eval.UndefinedError: None has no member named "proj_name"

I am not stopping there. So I read in the API Ref Doc that you can load a desired module into the context at Report class, and I tried, but it seems cannot works. My code is below.

class UserTimesheetReport(Report):
    "User Timesheet Report"
    __name__ = 'user.timesheet.report'
    
    @classmethod
    def get_context(cls, records, header, data):
        logger.warning(">>>>>>>> PROCESSING GET CONTEXT <<<<<<<")
        pool = Pool()
        Project = pool.get('afx.project')

        context = super().get_context(records, header, data)
        project_id = Transaction().context.get('project')
        project = Project(project_id) if project_id else None
        context['project'] = project
        return context

I click PRINT button, and I was hoping I can see the LOG Message in the console, but that also not happened (I already have a custom logging config file and executed when I start trytond).

My questions:

  1. Is there any link sample to do this that explains the proper way to use get_context method or any other workable solution that enable me to render the correct project name?
  2. Is there anyway I can print my logging message to console from overridden method?

Any advice would be very helpful to me

Regards
Bromo

You are close to the solution. The error just stats, that you have a record in records which has no project. No project means it is empty which is None. So you try to derive project_name from None(likeNone.proj_name`), which is not possible.
There are two solutions:

  1. Make the project field required on UserTimesheetRecord that it has always a value
  2. Catch the None project in the placeholder like this
<record.project.proj_name if record.project else ''>   or

<getattr(record.project, 'proj_name', '')>  or

<if test="record.project">
    <record.project.proj_name'>
</if>

WDYT?

I fixed this already.. I use cursor IDE and the AI shows me how to utilize get_context combined with execute methods.. thanks anyway @udono

regards
Bromo