This question has been flagged
5143 Views

Hi 

I am working on a project where they wanted the auto save of form views. I trying to make it with onchange method,In my code of onchange I given condition to create and write. but when I open the form view for the first time itself it's creating 19 new records as I given 19parameters in onchange.

I am pasting my code below.

Thanks in Advance.

    @api.onchange('name','partner_id','email','phone','date_order',\

'state','address_type','exact_street','exact_street2','exact_zip','first_name','last_name',\

'exact_city','exact_state_id','exact_country_id','temp_street','temp_street2','temp_zip','temp_city','temp_state_id','temp_country_id')

def onchange_partner_id(self):

if self.name == False:

self.name = self.env['ir.sequence'].next_by_code('wo.creation') or '/'

if self.partner_id:

self.update({

'email': self.partner_id.email

})

searched_id = self.search([('name','ilike', self.name)])

if searched_id:

searched_id.write({'partner_id': self.partner_id.id or False,

'date_order':self.date_order,

'state':self.state,

'name':self.name,

'email':self.email,

'phone':self.phone,

'address_type':self.address_type.id or False,

'exact_street':self.exact_street,

'exact_street2':self.exact_street2,

'exact_zip':self.exact_zip,

'exact_city':self.exact_city,

'exact_state_id':self.exact_state_id.id or False,

'exact_country_id':self.exact_country_id.id or False,

'temp_street':self.temp_street,

'temp_street2':self.temp_street2,

'temp_zip':self.temp_zip,

'temp_city':self.temp_city,

'temp_state_id':self.temp_state_id.id or False,

'temp_country_id':self.temp_country_id.id or False,

'last_name':self.last_name,

'first_name':self.first_name,

})

else:

self.create({'partner_id': self.partner_id.id or False,

'date_order':self.date_order,

'state':self.state,

'name':self.name,

'email':self.email,

'phone':self.phone,

'address_type':self.address_type.id or False,

'exact_street':self.exact_street,

'exact_street2':self.exact_street2,

'exact_zip':self.exact_zip,

'exact_city':self.exact_city,

'exact_state_id':self.exact_state_id.id or False,

'exact_country_id':self.exact_country_id.id or False,

'temp_street':self.temp_street,

'temp_street2':self.temp_street2,

'temp_zip':self.temp_zip,

'temp_city':self.temp_city,

'temp_state_id':self.temp_state_id.id or False,

'temp_country_id':self.temp_country_id.id or False,

'last_name':self.last_name,

'first_name':self.first_name,

})

return

@api.multi

 

@api.model

def create(self, vals):

if vals.get('name') == False:

vals['name'] = self.env['ir.sequence'].next_by_code('wo.creation') or '/'

return super(WorkOrder, self).create(vals)

@api.multi

def write(self, vals):

if vals.get('name'):

pdb.set_trace()

searched_id = self.search([('name','=', vals.get('name'))])

if not searched_id:

   return super(WorkOrder, self).write(vals)

Avatar
Discard