I have two one-to-one datasets.
I cannot combine two data sets because:
- Individual records may be present only in set A only in set B or in both sets A and in set B; and
- The relationship between the entries in set A and set B is temporary, which means that the entries can become connected and can become dissociated; and
- Data in set A is processed differently than data in set B; and
- There are external architectural restrictions.
When an entry in set A is associated with an entry in set B, I want to link the two entries. When records are linked, the relationship must be one-to-one. How can I guarantee that a one-to-one relationship?
The following code seems close, but I'm new to Odoo and don't know how to analyze if this approach guarantees a one-to-one relationship.
import openerp
class A(openerp.models.Model):
_name = 'set.a'
_sql_constraints = [
('set_b_id', 'unique("set_b_id")', 'Field set_b_id must be unique.'),
]
set_b_id = openerp.fields.Many2one(
comodel_name='set.b',
)
class B(openerp.models.Model):
_name = 'set.b'
set_a_id = openerp.fields.One2many(
comodel_name='set.a',
inverse_name='set_b_id',
)
@openerp.api.constrains('set_a_id')
def _constrains_set_a_id(self):
if len(self.set_a_id) > 1:
raise openerp.exceptions.ValidationError('Additional linkage failed.')
Another approach might be to openerp.fields extension to recreate the previously deprecated One2one relationship, but I'm not sure if this can be done cleanly.
source
share