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

Raise validation error is blocking the flow

By
nagarjuna
on 9/7/15, 9:28 AM 1,088 views

inside create method i have an if condition. in the condition i am performing some insertion. code is not giving any error but record is not inserting into the db, due to the last line raise validation error. if remove that and give return something then the record is inserting into db. how to avoid this ?

def create(self, cr, uid, vals, context=None):

reserve_item_id = vals['reserve_item_id' 

result = self.pool.get('item.item').browse(cr, uid, reserve_item_id)  

item_count = result.item_count

if result.item_count - result.reserve_count == 0:

vals={}

vals['mail_user_id']=uid

vals['mail_item_id']=reserve_item_id

vals['status']=False

cr.execute("INSERT INTO email_email (mail_user_id,mail_item_id) VALUES (%s, %s)", (uid, reserve_item_id))

raise ValidationError("No Item available")

Hi Pavan, my issues resolved after using curson.commit(). Thank you

nagarjuna
on 9/8/15, 7:11 AM
0

Pawan

--Pawan--
1267
| 4 3 5
Hyderabad, India
--Pawan--


Pawan
On 9/8/15, 1:35 AM

Nagarjuna,

After your cr.execute() statement, you can call "cr.commit()", that will save your record in the database, and let your exception call.

One more thing, inspite your record will be created in the database with the values you are inserting, but its better if you pass odoos default field values also, as create function didn't called successfully, these values will not be inserted.[create_uid, create_date, write_date, write_uid.]

Hope it helps


0
nagarjuna
On 9/7/15, 10:27 AM

Hi Yenthe,


Thanks for the reply, i have as like below

from openerp.modules.registry import RegistryManager

newcr = RegistryManager.get(cr.dbname).cursor()

self.pool.get('email.email').create(cr, uid, vals, context=context)

newcr.close()

still i am facing same issue, can you please help on this

If I'm not mistaking the problem is that you're using self.ppol.get which is still not using your new cursor. Your new record is still created with the default cursor? Do your cr.execute with new new cursor (newcr)

Yenthe
on 9/7/15, 11:01 AM
0

Yenthe

--Yenthe--
6676
| 7 7 8
Turnhout, Belgium
--Yenthe--

My Odoo blog: http://www.odoo.yenthevg.com/

My Twitter: https://twitter.com/Yenthe666

Yenthe
On 9/7/15, 10:05 AM

Hi Nagarjuna,

The simple answer is you can't with the way you're trying to. When Odoo runs through this function it will start checking things, you then do your cr.execute which works fine but the moment it hits your ValidationError it will do a roll-back. The default behaviour by Odoo is to do a transactional rollback when there is a ValidationError.
You could however avoid this by creating a separate transaction. Transactions are bound to cursors so you need to get a new cursor to the current database, something along the lines of:

newcr = RegistryManager.get(cr.dbname).cursor() 

With the new cursor you can do your transaction and then the ValidationError won't do a rollback since it is a separate cursor. (Don't forget to open and close that cursor).

Yenthe

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

2 follower(s)

Stats

Asked: 9/7/15, 9:28 AM
Seen: 1088 times
Last updated: 9/8/15, 7:11 AM