Skip to Content
Меню
Вам необхідно зареєструватися, щоб взаємодіяти зі спільнотою.
Це запитання позначене
3 Відповіді
2404 Переглядів

Hello,


Here is my problem, hope you can help me :


In a view, i've got a Many2one field (model A) called "type"

In this same view, i've got a One2many field (model B) and when i click on Add button, there is a form with an other Many2one field (model A) "type"


I want that when i open the form of the one2many field, that the value of field "type" is the same as the value of the field "type" of the main view


Is anybody know how to do that ?

Аватар
Відмінити
Найкраща відповідь

Hi,

I think we can make use of odoo's related field here to get the desired result.Here's a sample of the code that might be applicable in your case:


class MyMainTable(models.Model):
    _name = "my.main.view"

    sale_id = fields.Many2one('sale.order')

    line_ids = fields.One2many(
        comodel_name='my.new.table',
        inverse_name='main_table_id',
        string='Related New Table Records',
    )


class NewTableModel(models.Model):
    _name = 'my.new.table'
    _description = 'Description of your new table'


    sale_id = fields.Many2one(related='main_table_id.sale_id', readonly=False)

    main_table_id = fields.Many2one('my.main.view', string='Main Table Reference')

In the above, the sale_id can be considered as the many2one field from model A and 'my.new.table' as the relation model for the one2many field . So we can use the inverse field in the model B and  define the sale_id as a related field.

Hope it helps

Аватар
Відмінити
Автор

Hi Cybrosis,

thank you for your answer but it is too complicated to add a new field only for this.
I found another solution in adding context in the field one2many like that :

<field name="myone2many" mode="kanban" context="{'default_toto_id': totot_id}"> ...

Автор Найкраща відповідь

Hi Brain Station,

Thanks for your answer

I know how to pass context via action button

But the problem whith the button Add in a one2many, is that there is no action button or i don't know how to find it ?!!

Аватар
Відмінити
Найкраща відповідь

You can achieve this goal by passing context through the method of the button action. 


Like, 


Context = { ‘default_type’ = self.type } 


Then return the view action and context. 


Or in the xml file : 


Context=”{‘default_type’:type}” 

Thanks & Regards, 

 

Brain Station 23 Ltd. 

Mobile: (+880) 1404055226 

Email: sales@brainstation-23.com 

Web: https://brainstation-23.com/ 

Address: (Building-1) 8th Floor, 2 Bir Uttam AK Khandakar Road, Mohakhali C/A, Dhaka 1212, Bangladesh 

Аватар
Відмінити