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.

1

how to force write() method to load unchanged field values?

By
Abdullah Sofan
on 12/7/14, 6:26 AM 2,279 views

I just found out that write() method only loads vals of fields that have been changed or altered.  How can I force it to load a field that has not been changed or altered. 

In my code below I am trying to have the write() method to load prod_id value which is the id of the product created by the create() method.

[code]

class intrac_courses(osv.osv):

    def create(self, cr, uid, vals, context=None):
        if not vals:
            vals = {}
        if context is None:
           context = {}
        product_name = vals.get('name', '1')
        product_fee = vals.get('course_fee', '0.0')
        product_desc = vals.get('course_outline', '')
        product_obj = self.pool.get('product.template')
        product_type = 'service'
        vals['prod_id']= product_obj.create(cr, uid, {'name' : product_name, 'type': product_type, 'list_price': product_fee, 'description': product_desc }, context=context)
        return super(intrac_courses, self).create(cr, uid, vals, context=context)

    def write(self, cr, uid, ids, vals, context=None):
        product_id = vals.get('prod_id', 'unable to load product id')
        product_name = vals.get('name', 'unable to load product name')  #this is not able toload 
        product_fee = vals.get('course_fee', '0.0')
        product_desc = vals.get('course_outline', '')
        product_obj = self.pool.get('product.template')
        product_type = 'service'
        product_obj.write(cr, uid, [product_id], {'name' : product_name,'type': product_type, 'list_price': product_fee, 'description': product_desc }, context=context)
        return super(intrac_courses, self).write(cr, uid, ids, vals, context=context)
        

    _name = 'intrac.courses'
    _columns = {
        'prod_id': fields.char('Product ID'),
        'course_id': fields.char('Course ID'),
        'course_category': fields.selection([('General English','General English'),('Intensive English','Intensive English'),('English for Special Purposes','English for Special Purposes'),('Business Edge','Business Edge'),('Information Technology','Information Technology'),('Other','Other')], 'Course Category', required=True),
        'name': fields.char('Course Name', size=100, required=True),
        'course_arabic_name': fields.char('Arabic Name', size=100),
        'course_long_name': fields.char('Long Name', size=200, required=True),
        'course_duration': fields.integer('Duration (Hours)', required=True),
        'course_fee': fields.char('Fees Per Trainee'),
        'course_outline': fields.html('Course Outline & Details'),
        'course_notes': fields.one2many('intrac.courses.notes', 'course_name', 'Course Notes'),
    }
intrac_courses()

 [/code]

1
Michael Pol
On 12/7/14, 10:37 AM

In write method you will get ids of modified record. You can browse or read whatever you want fetch through ids

written = super(intrac_courses, self).write(cr, uid, ids, vals, context=context)
reads = self.read(cr, uid, ids, ['prod_id','name','course_fee','course_outline'], context=context)
product_obj = self.pool.get('product.template')
for record in reads:
    product_id = record['prod_id']
    if product_id:
        product_vals = {
            'name' : record['name'] or 'unable to load product name',
            'list_price' : record['course_fee'] or 0.0,
            'description' : record['course_outline'] or '',
            'type' : 'service'
        }
        product_obj.write(cr,uid,product_id,product_vals,context=context)
return written

 

Sorry Michael. Can you please show me how to modify the code to get the product ID?

Abdullah Sofan
on 12/7/14, 11:02 AM

I have modified my answer. Please test above above code.

Michael Pol
on 12/7/14, 1:18 PM

I tried the code above but I am getting AssertionError??!! Full Error as below. "OpenERP Server Error Traceback (most recent call last): File "C:\Program Files (x86)\Odoo 8.0-20140928-000204\server\.\openerp\http.py", line 499, in _handle_exception File "C:\Program Files (x86)\Odoo 8.0-20140928-000204\server\.\openerp\http.py", line 516, in dispatch File "C:\Program Files (x86)\Odoo 8.0-20140928-000204\server\.\openerp\http.py", line 282, in _call_function File "C:\Program Files (x86)\Odoo 8.0-20140928-000204\server\.\openerp\service\model.py", line 113, in wrapper File "C:\Program Files (x86)\Odoo 8.0-20140928-000204\server\.\openerp\http.py", line 279, in checked_call File "C:\Program Files (x86)\Odoo 8.0-20140928-000204\server\.\openerp\http.py", line 732, in __call__ File "C:\Program Files (x86)\Odoo 8.0-20140928-000204\server\.\openerp\http.py", line 375, in response_wrap File "C:\Program Files (x86)\Odoo 8.0-20140928-000204\server\openerp\addons\web\controllers\main.py", line 944, in call_kw File "C:\Program Files (x86)\Odoo 8.0-20140928-000204\server\openerp\addons\web\controllers\main.py", line 936, in _call_kw File "C:\Program Files (x86)\Odoo 8.0-20140928-000204\server\.\openerp\api.py", line 237, in wrapper File "C:\Program Files (x86)\Odoo 8.0-20140928-000204\server\openerp\addons\academy\academy.py", line 55, in write File "C:\Program Files (x86)\Odoo 8.0-20140928-000204\server\.\openerp\api.py", line 237, in wrapper File "C:\Program Files (x86)\Odoo 8.0-20140928-000204\server\openerp\addons\product\product.py", line 713, in write File "C:\Program Files (x86)\Odoo 8.0-20140928-000204\server\.\openerp\api.py", line 237, in wrapper File "C:\Program Files (x86)\Odoo 8.0-20140928-000204\server\openerp\addons\mail\mail_thread.py", line 427, in write File "C:\Program Files (x86)\Odoo 8.0-20140928-000204\server\.\openerp\api.py", line 237, in wrapper File "C:\Program Files (x86)\Odoo 8.0-20140928-000204\server\.\openerp\api.py", line 360, in old_api File "C:\Program Files (x86)\Odoo 8.0-20140928-000204\server\.\openerp\models.py", line 3693, in write File "C:\Program Files (x86)\Odoo 8.0-20140928-000204\server\.\openerp\api.py", line 235, in wrapper File "C:\Program Files (x86)\Odoo 8.0-20140928-000204\server\.\openerp\api.py", line 552, in new_api File "C:\Program Files (x86)\Odoo 8.0-20140928-000204\server\.\openerp\models.py", line 3935, in _write File "C:\Program Files (x86)\Odoo 8.0-20140928-000204\server\.\openerp\api.py", line 237, in wrapper File "C:\Program Files (x86)\Odoo 8.0-20140928-000204\server\.\openerp\models.py", line 3498, in step_workflow File "C:\Program Files (x86)\Odoo 8.0-20140928-000204\server\.\openerp\workflow\__init__.py", line 40, in trg_write File "C:\Program Files (x86)\Odoo 8.0-20140928-000204\server\.\openerp\workflow\service.py", line 37, in new File "C:\Program Files (x86)\Odoo 8.0-20140928-000204\server\.\openerp\workflow\helpers.py", line 13, in __init__ AssertionError

Abdullah Sofan
on 12/7/14, 2:32 PM

Thanks Michael for your answer. It worked. The error was solved when I changed 'prod_id' from fields.char to fields.integer. I appreciate it.

Abdullah Sofan
on 12/8/14, 8:35 AM
0
Abdullah Sofan
On 12/7/14, 9:54 AM

Help Anyone?

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: 12/7/14, 6:26 AM
Seen: 2279 times
Last updated: 3/16/15, 8:10 AM