RandomPerson
RandomPerson

Reputation: 790

Assign value from one class to another

So in class 1 for example I calculate a price and I want to assign this price to a field from class 2 for example.

How can I achieve this, I tried the following without success:

self.browse(cr, uid, ids[0]).task_id.xx_new_price = new_price

xx_new_price is a field from another class which I can access trough task_id. new_price just contains a number.

EDIT: Updated with code

class PurchaseOrder(osv.Model):
_inherit = 'purchase.order'

def wkf_confirm_order(self, cr, uid, ids, context=None):
    new_price = self.pool.get('price.calculation').calculate_price(cr, uid, ids, context=context)

    for purchase_order in self.browse(cr, uid, ids):
        if purchase_order.task_id:
            task = self.pool.get('project.task').browse(cr, uid, purchase_order.task_id)[0]
            task.write({'xx_new_price': new_price})

_columns = {
    'project_id': fields.many2one('project.project', 'Project'),
    'task_id': fields.many2one('project.task', 'Task')
}

So I inherit the wkf_confirm method from purchase order (I left all the standard code out for this example). Because when I confirm an order I want it to assign a value to another field from another class (in this case the to the field xx_new_price in project.task. new_price calculates a price and contains a float.

Upvotes: 0

Views: 317

Answers (1)

Bhavesh Odedra
Bhavesh Odedra

Reputation: 11143

For write a new value in table, you need to call write of ORM for that class.

First you should take a task_id from the current object and than assign a new_price

For example:

for project in self.browse(cr, uid, ids):
    if project.task_id:
        project.task_id.write({'xx_new_price': new_price})

Upvotes: 1

Related Questions