How in custom module odoo 9 before insert new record in database, check is record exists?
For example:
If in table project.task we have name "PARIS" and date_deadline "2017-01-01" in code below I need warning before execute...
vals = {'name': 'PARIS','date_deadline': '2017-01-01']}
create_new_task = http.request.env['project.task'].create(vals)
Or maybe try get count from project.task where name='PARIS' and date_deadline='2017-01-01' before click on button save!
Any simple solution?
@api.one
@api.constrains('date','time') #Time is Many2one
def _check_existing(self):
count = self.env['test.module'].search_count([('date','=',self.date),('time','=',self.time.id)])
print(self.date) #2017-01-01
print(self.time.id) #1
if(count >= 1):
raise ValidationError(_('DUPLICATE!'))
After try insert new record in database where is date = 2017-02-02 and time = 1 get this message:
duplicate key value violates unique constraint "test_module_time_uniq" DETAIL: Key (time)=(1) already exists.
Time exist but date is different! I need constrains for 2 value! In databse I have only one row where is date = 2017-01-01 and time = 1
test_module_time_uniq
). You just need to remove it, and it should work. – dgeorgiev