تخطي للذهاب إلى المحتوى
القائمة
لقد تم الإبلاغ عن هذا السؤال
4 الردود
16073 أدوات العرض

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,

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


الصورة الرمزية
إهمال
الكاتب أفضل إجابة

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)

أفضل إجابة

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

الصورة الرمزية
إهمال
المنشورات ذات الصلة الردود أدوات العرض النشاط
0
أكتوبر 24
1393
0
سبتمبر 23
2009
0
فبراير 23
1604
1
نوفمبر 21
2054
7
مايو 18
4269