This question has been flagged
2 Replies
7362 Views

Hello ,

I have to try to perform two operation on one click. like first i am update a record on Table-1 one2many field and thats value is create in another Table-2 one2many table. but my task is when value is update on Table-1 than it's value create in Table-2 and Table-1 updated record  is delete.

so my code is below in my code Table-1 is updated and Table-2 also updated but i am unable to  delete record from Table-1

def action_assign_button(self): 
active_ids = self._context.get('active_id')
res = self.env['weight.calculator'].browse(active_ids)
#Table-1
batch_obj = res.weight_cal_machine

for b_obj in batch_obj:  
    record_obj = b_obj.env['batch.num'].search([])
    for record in record_obj:
        if b_obj.select_pet == True: #select_pet = boolean field
        #Record update in Table-1       
            b_obj.write({'batch_number': self.batch_no.batch_no})
            if record.batch_no == b_obj.batch_number: 
                batch_history = { 'batch_obj': self.batch_no.id,
    'process_opt':b_obj.process_opt.id,
    'pet_load': res.sum,}    
               
                #Record create in Table-2
                create_batch_history = self.env['batch.record'].create(batch_history)
                print "batch_obj::::::::::::::;last", b_obj, b_obj.select_pet
                b_obj.unlink()

So it's show error on unlink.

so any solution. ???

Thanks in Advanced.

Avatar
Discard
Author

It's show error on terminal

AttributeError: 'unicode' object has no attribute 'unlink'

Best Answer

Hello Manish,

def action_assign_button(self):

active_ids = self._context.get('active_id')

res = self.env['weight.calculator'].browse(active_ids)

# Table-1

batch_obj = res.weight_cal_machine

for b_obj in batch_obj:

if b_obj.select_pet == True: # select_pet = boolean field

# Record update in Table-1

b_obj.write({'batch_number': self.batch_no.batch_no})

record_obj = b_obj.env['batch.num'].search([])

for record in record_obj:

if record.batch_no == b_obj.batch_number:

batch_history = {'batch_obj': self.batch_no.id,

'process_opt': b_obj.process_opt.id,

'pet_load': res.sum, }

# Record create in Table-2

create_batch_history = self.env['batch.record'].create(batch_history)

print "batch_obj::::::::::::::;last", b_obj, b_obj.select_pet

if record.batch_obj_1:

print "Lastttttttt", record.batch_obj_1

b_obj.unlink()

return

You can change your code according to above. It may help you.

Thanks.

Avatar
Discard
Author

Thanks @Pawan it's working...

Author

But can you tell me what is role of return in this case .. ?

When condition fulfil then return value