This question has been flagged
4 Replies
14454 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")

Avatar
Discard
Author

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

Best Answer

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


Avatar
Discard
Author Best Answer

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

Avatar
Discard

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)

Best Answer

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

Avatar
Discard