Hello Yaseen,
You can do this in two ways.
1. By Inheriting Create Method
2. sql_constraints
---
1. By Inheriting Create Method
From the create method, you can search in the DB, the record with the same values already exists or not.
Like This,
@api.model def create(self, vals): res = super(YourClass, self).create(vals) attendance_obj = self.env['your.model'] already_created_attendance = attendance_obj.search( [('related_field_1', '=', res.related_field_1.id), ('related_field_2', '=', res.related_field_2.id), ('date', '=', res.date), ('char_field_1', '=', res.char_field_1)]) if len(already_created_attendance) > 1: raise ValidationError(_('Attendance is already created')) return res |
Here in the example, I use related_field_1, char_field_1, etc.. you can use your fields instead.
2. _sql_constraints
_sql_constraints = [
('attendance_unique', 'unique(field_1, field_2, field_2, field_4)',
'Attendance Already Exist!'),
]
Thanks & Regards
Avinash Nadu Kandy
Mailtrack 27/11/19, 12:19:45
Mailtrack 27/11/19, 12:17:26