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')
我想从下拉列表('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/