Skip to Content
Meniu
Trebuie să fiți înregistrat pentru a interacționa cu comunitatea.
Această întrebare a fost marcată
2 Răspunsuri
8922 Vizualizări

I have models A and B:

class A (models.Model):
    _name = 'example.a'

    name = fields.Char(string="Model A")

    b_id = fields.Many2one('example.b', string='Model B')

class B (models.Model):

   _name = 'example.b'

   name = fields.Char(string="Model B")

    a_ids = fields.One2many('example.a', 'b_id', string="Models A")

I want to Add an item in a_ids from the existing records of A, not create new ones, how can this be achieved ? the autogenerated view for a_ids field only allows me to insert new records everytime I click Add an item, How can I show a list of existing records from the Many2One b_id field in model A. I though odoo will automagically show me a list of such values since the declaration of the field explicitly names the field in the Many2One relationship, thanks in advance

Imagine profil
Abandonează
Cel mai bun răspuns

Try this on your view to show existing records when u click Add an item

<field name="a_ids" widget="many2many"/>

Imagine profil
Abandonează
Cel mai bun răspuns

Actually what you wrote must work. I have something very similar in my extension of res.partner:

wo_ids = fields.One2many('inspection_tech.work_order',
                             inverse_name = "customer",
                             string="Work Orders", 
                             readonly=True)

In my partner view I have added the work order view in a tab:

<notebook position="inside">
                    <page string="Work Orders">
                        <field name="wo_ids"/>
 And it works as a charm. Are you sure you have  example.a records with b_id point to the viewed example.b instance?     

Imagine profil
Abandonează