1

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
)
foo
  • 21
  • 1
  • 4
  • Update one2many fields with the set of flags. look here https://hilarlive.wordpress.com/2017/04/22/one2many-or-many2many-flags/ – Hilar AK Mar 27 '18 at 08:11
  • @Burmesepythis Not sure I understand, can you be more clear on where I would update this? Thank you. – foo Mar 28 '18 at 00:11

2 Answers2

1

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)
  • Thank you, this alone didn't seem to work. I wonder if it's because it's a computed field, or if it's due to some inner working with res.partner (I suspect the former). If I edit the record, modify a field, then save and close (dialog for this field), I can re-open it and I see the change. But if I save the parent record as a whole, and re-open this field - it doesn't seem to save it. – foo Mar 28 '18 at 00:00
1

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
Charif DZ
  • 14,415
  • 3
  • 21
  • 40
  • Thank you for sharing. I updated my code (see Update). It doesn't seem to work, despite adding the inverse name. Do you happen to see what I did incorrectly? Also, if many2many is the way to go - why is it better? – foo Mar 28 '18 at 00:13
  • It's better because i don't have to create a many2one in other model, i can use many2many widget in tree view to display data. Now when you change child_ids the the one2many field is loaded with records and when you save the records goes away, or no matter what you do the one2many is always emty. – Charif DZ Mar 28 '18 at 03:18
  • And make sure to upgrade your module to add the new field in the model. Your code should work – Charif DZ Mar 28 '18 at 03:19
  • I appreciate you sharing. I did upgrade my model, it's still not working. To clarify, this is on res.partner. By doing "@api.depends('child_ids')" I thought it would only show contacts if child_ids exist (eg. if there are some records in child_ids). I don't think this has to do with "changing child_ids" – that would be onchange as I understand it. Maybe child_ids is the issue and I want to remove that completely? – foo Mar 28 '18 at 17:24