Skip to Content
Menu
This question has been flagged
2 Replies
874 Views

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


Avatar
Discard
Author

please, can anybody help for this requirement ?

Best Answer

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 !




Avatar
Discard
Author

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

Best Answer

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

Avatar
Discard
Author

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

Author

any help for this ???

it is saying i asked 5 days ago...

regards

Related Posts Replies Views Activity
1
Apr 16
2691
3
Oct 15
11200
2
Feb 25
7773
1
Apr 24
992
1
Oct 23
8164