This question has been flagged
1 Reply
5199 Views

The write function is not updating while raising an error

    @api.model    
    def create(self,vals):
        record = self.search([('create_uid', '=', self.env.user.id), ('purchase_order_type', '=', 'po_type_local'), ('state', 'not in', ['sent', 'draft'])])               
        for rec in record:
            d1 = datetime.strptime(rec.date_order, '%Y-%m-%d %H:%M:%S')           
            d2 = datetime.strptime(fields.Date.today(), '%Y-%m-%d')           
            d3 = fields.Date.today()           
            daysDiff = str((d2-d1).days+1)
                if self.env.user.lpo_extend < d3:
                    if int(daysDiff) > 2:
                        if rec.invoice_count == 0:
                            self.env.user.write({'lpo_block': True})                       
                            raise UserError("You cannot create this order! Previous LPO is not processed yet!")           
                count = 0           
                if self.env.user.lpo_extend < d3:
                    for line in rec.invoice_ids:
                        if line.state != 'draft':
                            count += 1               
                    if int(daysDiff) > 2:
                        if count == 0:
                            self.env.user.write({'lpo_block': True})                       
                            raise UserError("You cannot create this order! Previous LPO is not processed yet!")       
        return super(PurchaseOrder, self).create(vals)
Avatar
Discard
Best Answer

Hi Jithin

That is an expected behavior in Odoo and many other transationals systems where a transaction is rolled back when an exception occurred to prevent data inconsistencies. If you really wanna make the write commited you have 2 choices

1- Commit the cursor by yourself wish could lead to previous changes in the data model to be persisted even when a rollback due to an error detected will be performed by Odoo 

self.env.cr.commit()

2- Use another cursor for that specific write, for example(more easy with the old api style):

with self.pool.cursor() as new_cr:
    self.pool.get('res.users').write(new_cr, [self.env.user.id], {'lpo_block': True})

Hope that helps

Avatar
Discard
Author

Could you be more specific. Can you show any related modules.

Mail me in k.jithin20@gmail.com

I update the answer with examples