1
votes

When I edit a record from this field (code below), it doesn't save for some reason. It's a computed field, linking to res.partner records. If I edit it and click save, it doesn't save at all (no changes in the database and/or if I hard refresh the page). Does someone see something here that I'm missing? If I can't edit it via what I'm expecting, is there another way to do this? The reason I do a computed field and not a domain on child_ids is because child_ids field with a domain doesn't seem to work properly with this domain.

Model

contact_ids = fields.One2many(comodel_name='res.partner', compute="_get_contact_ids", readonly=False)

@api.multi
@api.depends('child_ids')
def _get_contact_ids(self):
    for company in self:
        if company.child_ids:
            company.contact_ids = company.child_ids.search([('is_location', '=', False), ('parent_id', '=', company.id), ('type', '=', 'contact')])

View

<field name="contact_ids" string="Contacts">
    <tree create="true" delete="false" edit="true" default_order="create_date">
        <field name="name"/>
        <field name="phone"/>
        <field name="email"/>
    </tree>
</field>

Update

Added this per ideas, but it didn't work. Keep in mind, this is on a model that inherits res.partner.

activity_contact_id = fields.Many2one('res.partner', string="Contact")
contact_ids = fields.One2many(
    comodel_name='res.partner',
    inverse_name='activity_contact_id',
    compute="_get_contact_ids",
    readonly=False,
    stored=True
)
2
Update one2many fields with the set of flags. look here hilarlive.wordpress.com/2017/04/22/one2many-or-many2many-flagsHilar AK
@Burmesepythis Not sure I understand, can you be more clear on where I would update this? Thank you.foo

2 Answers

1
votes

Computed fields in Odoo are not stored by default, you need to set store=True in order to save the fields to database.

contact_ids = fields.One2many(comodel_name='res.partner', compute="_get_contact_ids", stored=True, readonly=False)
1
votes

To store a one2many value in database you need the inverse_name on the other model. I mean that you need to create a many2one field to save the id of the current record in the co_model. (o2m needs m2o you cannot store the values without m2o !! remember this role)

don't use one2many field use many2many field it is better.

   contact_ids = fields.Many2many(comodel_name='res.partner',
                                  relation="your_model_res_partner_rel", # always mention the name of the relation good practice
                                  column1 = "you_mode_id",
                                  column2 = "partner_id",
                                 compute="_get_contact_ids",
                                 store=True) # make your field stored no need for readonly it's by default

   @api.depends('child_ids')
   def _get_contact_ids(self):
        """ always explain what the method do here good practice for team work"""
        for company in self:
            if company.child_ids:
                # break you line when it's to long to be readable
                ids = company.child_ids.search([('is_location', '=', False),
                                                ('parent_id', '=', company.id),
                                                ('type', '=', 'contact')]).ids
                company.contact_ids = [(6, False, ids)] # replace all records by the new ids