Skip ke Konten
Menu
Pertanyaan ini telah diberikan tanda
2 Replies
875 Tampilan

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
Buang
Penulis

please, can anybody help for this requirement ?

Jawaban Terbai

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
Buang
Penulis

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

Jawaban Terbai

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
Buang
Penulis

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

Penulis

any help for this ???

it is saying i asked 5 days ago...

regards

Post Terkait Replies Tampilan Aktivitas
1
Apr 16
2694
3
Okt 15
11201
2
Feb 25
7773
1
Apr 24
993
1
Okt 23
8165