I have an entity teacher with a Many2One field "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? 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")
Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:
- ลูกค้าสัมพันธ์
- e-Commerce
- ระบบบัญชี
- สินค้าคงคลัง
- PoS
- Project
- MRP
คำถามนี้ถูกตั้งค่าสถานะ
2
ตอบกลับ
2200
มุมมอง
You can do it using constrains:
from odoo.exceptions import ValidationError
@api.constrains('substitute_teacher_id')
def _check_substitute_teacher(self):
for teacher in self:
if teacher.id == teacher.substitute_teacher_id.id:
raise ValidationError('You cannot set yourself as Substitute Teacher.')
This will check the Substitute Teacher field and raise the message to the user if user has set himself/herself as Substitute Teacher.
Hi Ernesto:
You can define a domain for the Many2one field like so
substitute_teacher_id = fields.Many2one('school.teacher', string="Substitute", domain="[('id', '!=', id)]")
สนุกกับการพูดคุยนี้ใช่ไหม? เข้าร่วมเลย!
สร้างบัญชีวันนี้เพื่อเพลิดเพลินไปกับฟีเจอร์พิเศษและมีส่วนร่วมกับคอมมูนิตี้ที่ยอดเยี่ยมของเรา!
ลงชื่อ