python - Odoo,如何从 many2one 字段中隐藏一个项目?

标签 python odoo odoo-10 many-to-one

Odoo-10

我的.py

class komMo(models.Model):
    _name = 'kom.mo'

    mo_id = fields.Integer(string='Code mo') #this is just the recognition number
    name = fields.Char(string='Name mo') 

    parent_id = fields.Many2one('kom.mo')

Form for editing

我想从下拉列表('parent_id')中隐藏选项(示例),如果这是对象本身的名称

因此,当我要编辑“示例”时,我不想在“parent_id”字段中作为选项提供

当我创建一个新的“example2”时,一切都很好,因为只有现有的项目显示在下拉列表中。

如果我不清楚请告诉我。 我的 .xml 文件非常基本,我没有添加任何选项或属性

最佳答案

只需将此域添加到字段 domain="[('id', '!=', id)]"。这将为它自己的形式移除对象。

您还可以使用odoo的嵌套集合系统来处理父子关系,这对解决父子关系查询有很大好处,通过在模型定义中设置_parent_store = True,并添加parent_left, parent_right 字段,您还可以在 parent_id 上使用 @api.constraint 调用 odoo 模型 _check_recursion 以确保没有递归父子关系创建。 例如在 odoo Product category 模型上:

class ProductCategory(models.Model):
    _name = "product.category"
    _description = "Product Category"
    _parent_name = "parent_id"
    _parent_store = True
    _parent_order = 'name'
    _rec_name = 'complete_name'
    _order = 'parent_left'

    parent_id = fields.Many2one('product.category', 'Parent Category', index=True, ondelete='cascade')
    parent_left = fields.Integer('Left Parent', index=1)
    parent_right = fields.Integer('Right Parent', index=1)

    @api.constrains('parent_id')
    def _check_category_recursion(self):
        if not self._check_recursion():
            raise ValidationError(_('Error ! You cannot create recursive categories.'))
        return True

关于python - Odoo,如何从 many2one 字段中隐藏一个项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53946292/

相关文章:

javascript - 如何在标题按钮内为 formview 编写事件?奥多10

python - 使用递归在 Python 中生成给定长度的所有二进制字符串的最佳方法是什么?

python - 如何在 OpenERP 中单击按钮后禁用它

javascript - Odoo 表单中的表单小部件按钮

web - 如何在odoo中更改日期选择器的默认星期几?

python - 模型 "res.config.settings"中不存在字段

java - 如何在 ubuntu 20.04 上构建和安装 pylucene

python - 如果else语句拒绝工作,则一行

Python分区和拆分

python - 在 Python 3.4 中使用 Odoo API 导入图像