How to create One2one relationships in Odoo 8?

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.'),
  ]

  # Constrained to be unique (see SQL above) which essentially changes
  # this end of the Many2one relationship to a One2one relationship. (The
  # other end of the relationship must also be constrained.)

  set_b_id = openerp.fields.Many2one(
    comodel_name='set.b',
  )

class B(openerp.models.Model):

  _name = 'set.b'

  # Constrained to tie with either zero keys or one key (see function
  # below) which essentially changes this end of the One2many
  # relationship to a One2one relationship. (The other end of the
  # relationship must also be constrained.)

  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.

+4
source share
1 answer

In your case, in principle, the one-to-one relationship is not available in Odoo 8.0, its completely outdated version 7.0 or later in Odoo (formally OpenERP).

, , , "--" many2one , .

, :)

+1

Source: https://habr.com/ru/post/1609131/


All Articles