Skip to Content
Menu
This question has been flagged
1 Reply
8073 Views

In a v8 installation with analytic accounting feature, I have been asked to show on the analytic account form view (analytic.view_account_analytic_account_form) a link to the related project.

On the server side I have to browse the project model searching a project having as analytic account the current one, I suppose something like (I use the limit=1 since the relation between project and analytic_account is an o2o):

project = self.env['project.project'].search([('analytic_account_id', '=', self.id)], limit=1)

How could I pass this value to the view, in order to show a link?

Should I use a context? and how?

Avatar
Discard
Best Answer

Option 1:

you can make new many2one computed field and reuse your code, that you posted in the question, inside the compute function for a new many2one field.

.py:

project_id = fields.Many2one(comodel_name="project.project", string="Project", compute="_compute_project_id")

@api.one
def _compute_project_id(self)
    self.project_id = self.env['project.project'].search([('analytic_account_id', '=', self.id)], limit=1) #I simply copied your code here

.xml:

<field name="project_id" />

Option 2:

you can make new one2many field, connecting to the project and in the inverse_name parameter, put the  analytic_account_id, so it'll be automatically find it's other end. It should work, if as you stated the relation is one2one in the practice, and you can then force this field to display as a link, instead of a table, by explicitly putting widget="many2one" in the xml definition of view.

.py:

project_id_s = fields.One2many(comodel_name="project.project", inverse_name="analytic_account_id", string="Project")

.xml:

<field name="project_id_s" widget="many2one" />


There may be more options, but one of the two should work for you I think.

Avatar
Discard
Author

in any case you suggest to add a new relational computed field. I was of the same opinion, thanks

yes, I suggested computed field but only in "Option 1". If you choose "Option 2", than there is no computed field, but only normal one.

Related Posts Replies Views Activity
0
Oct 24
1140
1
Nov 23
2526
2
Mar 19
5139
2
Mar 15
4233
2
Feb 25
950