Skip to Content
Menu
Musisz się zarejestrować, aby móc wchodzić w interakcje z tą społecznością.
To pytanie dostało ostrzeżenie
2 Odpowiedzi
1729 Widoki

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


Awatar
Odrzuć
Autor

please, can anybody help for this requirement ?

Najlepsza odpowiedź

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 !




Awatar
Odrzuć
Autor

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

Najlepsza odpowiedź

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

Awatar
Odrzuć
Autor

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

Autor

any help for this ???

it is saying i asked 5 days ago...

regards

Powiązane posty Odpowiedzi Widoki Czynność
1
kwi 16
3329
3
paź 15
11827
2
lut 25
8688
1
kwi 24
1743
1
paź 23
8938