Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
1 Trả lời
8222 Lượt xem

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?

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

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.

Ảnh đại diện
Huỷ bỏ
Tác giả

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.

Bài viết liên quan Trả lời Lượt xem Hoạt động
0
thg 10 24
1367
1
thg 11 23
2638
2
thg 3 19
5280
2
thg 3 15
4383
2
thg 2 25
1080