Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

add records to One2many relation from existing values in correspondent Many2one relation?

By
Nehemias Herrera
on 12/8/14, 6:06 PM 1,402 views

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

1
Sajin Aziz
On 12/9/14, 12:51 AM

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

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

0
Tzin
On 12/8/14, 10:34 PM

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?     

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 12/8/14, 6:06 PM
Seen: 1402 times
Last updated: 3/16/15, 8:10 AM