Odoo Help

Welcome!

This community 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.

0

The write function is not updating while raising an error

By
Jithin
on 11/20/16, 11:42 PM 107 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

--Axel Mendoza--
10238
| 7 7 8
Camaguey, Cuba
--Axel Mendoza--

DevOps - Full stack - Software Architect - Developer - Technology Integrator

I could help you to develop anything and solve complex problems based on technologies, integrations and tricky stuffs mostly in Python with OpenERP/Odoo, Zato, Django and many others frameworks programming languages and technologies.

I offers consulting services to anyone with an unanswered questions or needs for customizations. Think about it, maybe it's better to have an expert to solve your issues and projects than having a full time employee trying to understand what to do an how

Reach me at aekroft@gmail.com

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

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

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