I using Odoo14,
I create a new custom model that inherit email field from res.partner, and want to make email for each of my users which is students be unique.
Here is my snipet of code:
class AqurStudent(models.Model):The code above seems doesn't have any effect, I still can duplicate my students email and doesn't raise a ValidationError.
_name = "aqur.student"
_description = "AQUR Student"
_inherit = ['mail.thread', 'mail.activity.mixin']
_inherits = {"res.partner": "partner_id"}
partner_id = fields.Many2one('res.partner', 'Partner', required=True, ondelete="cascade")
@api.constrains('email')
def _check_email(self):
for record in self:
if record.email == record.email:
raise ValidationError('The email already registered, please use another email!')
res_partner = self.env['res.partner'].search_count([('email', '=', self.email)])
if len(res_partner) > 1:
raise ValidationError('The email already registered, please use another email!')
_sql_constraints = [
('email_key', 'UNIQUE (email)', 'The email already registered, please use another email!')
]
So, whats wrong with my code above?
Please, any help, source or tutorial how to face this will be very thanks,
Thanks,
Tri Nanda