繁体   English   中英

如何在OpenERP / Odoo上设置此特定域?

[英]How to set this specific domain on OpenERP/Odoo?

我在Odoo中有下一个表,名为关系,该表来自表女孩和表男孩之间的关系:

| girl_id | boy_id | 关系类型|

| 1 | 2 | 朋友|

| 1 | 3 | 兄弟姐妹|

| 2 | 7 | 恋人|

所以:

  • 在表女孩中,存在一个字段关系,该字段关系指向表关系。
  • 在桌男孩中,存在一个字段关系,该字段关系指向表关系。
  • 在表关系中,有两个字段girl_id和boy_id,它们都分别指向表girl和boy。

场景:

以女孩和男孩的形式存在领域关系。 当我为女孩或男孩添加新关系时,将打开一个表格以填写表关系的字段(girl_id,boy_id和Relationship_type)。 想象我是一个女孩的形式,我单击添加一个新的恋人,并且形式被打开。 为了避免看到girl_id(它是不可见的,但其中包含当前女孩的ID),我实施了此操作。 所以我只能看到两个字段(boy_id和Relationship_type)。

我想要的是:

继续执行该示例,如果我打开boy_id的下拉列表,则将看到所有男孩,甚至包括已经与此女孩相关的男孩。 例如,如果我要向ID为1的女孩添加关系,则不得看到ID为2和3的男孩,如果女孩为ID为2的男孩,则不得看到ID为7的男孩。

我的尝试

我在表关系中创建了两个字段,分别名为boys_of_the_girl(与“ girl_id.relationships”相关的一个)和girls_of_the_boy(与“ boy_id.relationships”相关的多个)。

我的代码:(示例:为一个女孩建立关系)

<field name="girl_id" invisible="1"/>
<field name="boys_of_the_girl" invisible="1"/>
<field name="boy_id" domain="[('id', 'not in', boys_of_the_girl)]"/>
<field name="relationship_type"/>

错误:

RuntimeError:调用Python对象时超出了最大递归深度

有人可以帮我吗? 谢谢!

编辑

男孩

relationships = fields.One2many(comodel_name='relationship',
                                inverse_name='boy_id',
                                string='Relationships')

女孩

relationships = fields.One2many(comodel_name='relationship', inverse_name='girl_id', string='Relationships')

关系

boy_id = fields.Many2one(comodel_name='boy', string='Boy', required=True)
girl_id = fields.Many2one(comodel_name='girl', string='Girl', required=True)
relationship_type = fields.Char(string='Relationship type')

好吧,最后我发现不可能通过XML代码来管理它,但是可以通过Python实现相同的目的:

仅使用此功能(其他形式类似于girl_id的域,另一种形式):

@api.onchange('girl_id')
def on_change_girl_id(self):
    current_girl_id = self.env.context.get('default_girl_id', False)
    relationship_recordset = self.search([('girl_id', '=', current_girl_id)])
    boy_recordset = relationship_recordset.mapped('boy_id')
    boy_ids = boy_recordset.mapped('id')
    boy_id_domain = [
        ('id', 'not in', boy_ids)
    ]
    result = {
        'domain': {
            'boy_id': boy_id_domain,
        },
    }
    return result

这样,就不必在XML表单中将任何域添加到字段boy_id中。 该函数将影响您为女孩设置关系的形式,正如我上面所写的,在为男孩设置关系时,必须声明另一个类似的函数来管理正确的行为。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM