Skip to Content
Menu
This question has been flagged
1 Reply
804 Views
 I have this odoo py


class CrmProject(models.Model):

_name='crm.project'

customer_id = fields.Many2one('res.partner','Customer')

project_product_id = fields.Many2one('crm.project.product','Project Product')

how to validate and show a warning when the customer_id and project_product_id are inputted together with the same value as the one in the crm.project database? so it must be checked with the database first then show warning if it have the same value with the one in customer_id AND project_product_id input.

For example:


Database table crm.project

customer_id = 1

project_product_id = 2

Will create warning ONLY if input:


customer_id = 1

project_product_id = 2

Beside that no warning will be create


I have tried this but no warning created:


@api.model
def create(self,vals):
vals['name'] = self.env['ir.sequence'].next_by_code('crm.project')
res = super(CrmProject,self).create(vals)
# Add code here
#res= super(CrmProject,self).create(vals)
customer_id = vals.get('crm.project.customer_id')
project_product_id = vals.get('crm.project.project_product_id')
get_customer_id = self.env['crm.project'].search([('customer_id','=',customer_id)])
get_project_product_id = self.env['crm.project'].search([('project_product_id','=',project_product_id)])
if get_customer_id and get_project_product_id:
raise UserError(_('The project has already been set before'))
else:
return res

Any help will be grateful

Avatar
Discard
Best Answer

Use constraint

_sql_constraints =[

('unique_crm_project',
'UNIQUE(customer_id ,project_product_id )',
"CRM must be unique"),
]
Avatar
Discard