Odoo Help

0

The write function is not updating while raising an error

By
Jithin
on 11/20/16, 11:42 PM 343 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)
0
Axel Mendoza
On 11/21/16, 2:53 AM

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

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

Mail me in k.jithin20@gmail.com

Jithin
on 11/21/16, 2:58 AM

I update the answer with examples

Axel Mendoza
on 11/21/16, 3:19 AM

About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

Register

Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

1 follower(s)

Stats

Asked: 11/20/16, 11:42 PM
Seen: 343 times
Last updated: 11/21/16, 2:53 AM