تخطي للذهاب إلى المحتوى
القائمة
لقد تم الإبلاغ عن هذا السؤال
2 الردود
1924 أدوات العرض

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
أبريل 16
3532
3
أكتوبر 15
12065
2
فبراير 25
8966
1
أبريل 24
2009
1
أكتوبر 23
9216