2015-09-26 137 views
4

我有兩組數據展現出一對一的關係。如何在Odoo 8中創建One2one關係?

我不能給兩組數據合併的原因是:

  1. 特定記錄可能只集合A中的存在,只是在集B,或兩者在集A和集B;和
  2. 集合A和集合B中的記錄之間的關聯是暫時的,這意味着記錄可以關聯並且可以變爲關聯;和
  3. 集合A中的數據處理方式與集合B中的數據不同;和
  4. 有外部架構限制。

當集合A中的記錄與集合B中的記錄相關聯時,我想鏈接這兩個記錄。當記錄被鏈接時,關係必須是一對一的。 我如何保證這種關係是一對一的關係?

下面的代碼看起來很接近,但我對Odoo的工作很陌生,並且不確定如何分析這種方法是否可以保證一對一的關係。

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

另一種方法可能會延長openerp.fields重新創建之前已停用One2one關係,但我不能肯定可以做乾淨。

回答

1

在你的情況下,基本上一對一的關係在Odoo 8.0中不可用,它在Odoo中完全不贊成使用一個7.0或更高版本(正式的OpenERP)。

所以請我的建議是,不要使用一對一的關係只是使用它作爲many2one而不是使用它,並設置您的本地根據您的需要。

我希望我的回答對你有所幫助:)