Skip to Content
Meniu
Trebuie să fiți înregistrat pentru a interacționa cu comunitatea.
Această întrebare a fost marcată
2 Răspunsuri
5072 Vizualizări

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):
_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!')
]
The code above seems doesn't have any effect, I still can duplicate my students email and doesn't raise  a ValidationError.


So,  whats  wrong  with  my  code  above?

Please,  any  help,  source  or  tutorial  how  to  face  this  will  be  very  thanks,


Thanks,

Tri  Nanda 

Imagine profil
Abandonează
Autor Cel mai bun răspuns

Finanly I solved this isue using this following code:

class Partner(models.Model):
_inherit = "res.partner"

@api.constrains('email')
def _check_email(self):
count_email = self.search_count([('email', '=', self.email)])
if count_email > 1 and self.email is not False:
raise ValidationError('The email already registered, please use another email!')

I don't find the way yet, how to use constrains on inherit model, so then I create the constrains code on the model that store the email field directly.


Thanks,

Tri Nanda


Imagine profil
Abandonează
Cel mai bun răspuns

You can use the code as below,

@api.model
def create(self, vals):
    previous_email = self.env['aqur.student'].search([('email','=',vals['email'])])
    if len(previous_email) >= 1:         raise ValidationError(_("This Email is Already Taken"))


Try to follow the above techniques, i hope this will solve your issue.



Imagine profil
Abandonează
Related Posts Răspunsuri Vizualizări Activitate
1
oct. 22
4864
2
ian. 24
4861
0
iul. 23
2211
1
aug. 22
4292
0
feb. 22
3435