跳至內容
選單
此問題已被標幟
2 回覆
1937 瀏覽次數

i had a model say mymain.py which contains fields:

id, name, description, status

1) i want to show 'name' field in another model (mymodel2.py) many2one field -btw its by default working fine

2) but simultaneously, in one more model (mymodel3.py) i want to show 'description' field in its many2one field.

please recommend the best way to deal this and how to for both.

regards


頭像
捨棄
作者

please, can anybody help for this requirement ?

最佳答案

In mymodel3, you will need to pass a context on the field which you are creating as a relational field to the mymain model and define a _compute_display_name method which will display the description instead of the name on the relational field.

class MyModel3(models.Model):
_name = 'my.model3'

@api.depends_context('show_desc')
def _compute_display_name(self):
for record in self:
desc = self.env.context.get('show_desc', False)
name = record.description if desc and record.description else record.name
record.display_name = name

mymain_desctiption_id = fields.Many2one('my.main', 'Description from MyMain', context={'show_desc': True})


Try this out this may work for you !




頭像
捨棄
作者

thanks very much @Adnan, will check and confirm, this time in a pressure at work. struggling with odoo 15 and odoo 16 , raising question here.

with best regards

最佳答案

Hi, 

You can achieve this by using the related attribute in the fields definition. Here's how you can implement it for both mymodel2.py and mymodel3.py:

Assuming your mymain.py looks like this:

from odoo import models, fields

class MyMain(models.Model):
    _name = 'my.main'

    name = fields.Char(string='Name')
    description = fields.Text(string='Description')
    status = fields.Char(string='Status')

Now, for mymodel2.py:

from odoo import models, fields class MyModel2(models.Model): _name = 'my.model2' name_id = fields.Many2one('my.main', string='Name', required=True) name = fields.Char(related='name_id.name', string='Related Name', store=True)

And for mymodel3.py:

from odoo import models, fields

class MyModel3(models.Model):
    _name = 'my.model3'

    description_id = fields.Many2one('my.main', string='Description', required=True)
    description = fields.Text(related='description_id.description', string='Related Description', store=True)


Hope it helps

頭像
捨棄
作者

thanks @Cybrosys

still confusion is there...

for mymodel3 i asked to have many2one field which should show 'description' field to select from my.main model not 'name' field which is default.

hope its clear now.

regards

作者

any help for this ???

it is saying i asked 5 days ago...

regards

相關帖文 回覆 瀏覽次數 活動
1
4月 16
3537
3
10月 15
12077
2
2月 25
8975
1
4月 24
2017
1
10月 23
9228