This question has been flagged
3 Replies
12478 Views

Can I add a field to many2many relation table

Avatar
Discard
Best Answer

Yes, you have to do this:

class res_partner(osv.osv)
...
    'category_ids': fields.many2many('res.partner.category','res_partner_category_rel','partner_id','category_id','Categories'),
...

class partner_category_rel(osv.osv):
    _name = "res.partner.category.rel"
    _rec_name = "partner_id"
    _columns = {
        'partner_id': fields.many2one('res.partner', 'Partner', ondelete='cascade'),
        'category_id': fields.many2one('res.partner.category', 'Category', ondelete='cascade'),
        'additional_field': fields.integer('Other field'),
    }

partner_category_rel()
Avatar
Discard
Best Answer

But, this, only work if the relational class was defined before many2many fields!

Avatar
Discard
Best Answer

This should work (I used this in OERP 7)

class left_model(osv.osv):
_name = 'left.model'


_columns = {
'rigths': fields.many2many(obj='right.model.id', rel='left_model_right_model_rel', id1='left_model_id', id2='right_model_id', string='Rigths'),
}

left_model()


class right_model(osv.osv):
_name = 'right.model'


_columns = {
'lefts': fields.many2many(obj='left.model.id', rel='left_model_right_model_rel', id1='right_model_id', id2='left_model_id', string='Lefts'),
}

right_model()


class center_model(osv.osv):
_name = 'center.model'
_table = 'left_model_right_model_rel' # this should match the rel argument in the other two models OR what ever OERP auto created.


_columns = {
'left_model_id': fields.many2one(obj='left.model.id', string='Left Model ID'),
'right_model_id': fields.many2one(obj='right.model.id', string='Right Model ID'),
'new_field': fields.char(string='New Field'), # this is your new field, customize it to your needs
}

center_model()

Please let me know if I made a typo or somethings askew :-)

Avatar
Discard