简体   繁体   English

如何在 odoo 中将域设置为 Many2One 字段?

[英]How to set domain to Many2One fields in odoo?

I have an entity teacher with a Many2One field "substitute_teacher_id".我有一个带有 Many2One 字段“substitute_teacher_id”的实体老师。 The idea is that a teacher can replace other teacher, but a teacher should not be able to replace himself.这个想法是,一个老师可以代替其他老师,但一个老师不应该能够代替自己。 How can I set that restriction in the teacher's model and/or view to ensure that a teacher cannot select a teacher with the same id as a substitute?我如何在老师的 model 和/或视图中设置该限制以确保老师不能 select 具有相同 ID 的老师作为替代老师? This is my code这是我的代码

class Teacher(models.Model):
    _name = 'school.teacher'
    _description = 'School Teacher'

    name = fields.Char(string="Name")
    substitute_teacher_id = fields.Many2one('school.teacher', string="Sustitute")

You need to expose id field before substitute_teacher_id field in from view.您需要在视图中的substitute_teacher_id字段之前公开id字段。

Now add following domain in it:现在在其中添加以下域:

<field name="substitute_teacher_id" domain="[('id', '!=', id)]"/>

EDIT:编辑:

<field name="id" invisible="1"/>
<field name="substitute_teacher_id" domain="[('id', '!=', id)]"/>

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

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