manuthalasseril
manuthalasseril

Reputation: 1064

How to access a value of a many2one field from a form submitted in openerp?

I have a quotation form with a many2one field (x_delivery_periods) in openerp. Before write to database, I want to calculate the committed date using the function field (commitment_date). So I write the following code.

def _get_commitment_date(self, cr, uid, ids, name, arg, context=None):
    res = {}
    dates_list = []
    deliveryperiod_obj = self.pool.get('stock.deliveryperiods')
    for order in self.browse(cr, uid, ids, context=context):
        dates_list = []

        deliveryperiod_read_test = deliveryperiod_obj.read(cr, uid, 4, ['name'], context) #working
        deliveryperiod_browse_test = deliveryperiod_obj.browse(cr,uid,4).name #working
        print deliveryperiod_read_test #working displays 10.0
        print deliveryperiod_browse_test #working displays {'name': 10.0, 'id': 4}

        print order.x_deliveryperiod #working displays browse_record(stock.deliveryperiods, 4)
        deliveryperiod_read = deliveryperiod_obj.read(cr, uid, order.x_deliveryperiod, ['name'], context) #not working TypeError: argument 2 to map() must support iteration
        deliveryperiod_browse = deliveryperiod_obj.browse(cr,uid,order.x_deliveryperiod).name #not working TypeError: argument 2 to map() must support iteration
        print deliveryperiod_read
        print deliveryperiod_browse

        dt = datetime.strptime(order.date_order, '%Y-%m-%d') + relativedelta(days=deliveryperiod_read or 0.0)
        dt_s = dt.strftime('%Y-%m-%d')
        dates_list.append(dt_s)
        if dates_list:
            res[order.id] = min(dates_list)
    return res

_columns = {
    'commitment_date': fields.function(_get_commitment_date, store=True, type='date', string='Commitment Date', help="Committed date for delivery."),
}

But while fetching the data from self.browse oblect results the above errors. Any help will be highly appreciated . Thanks in advance.

Upvotes: 0

Views: 986

Answers (1)

Bhavesh Odedra
Bhavesh Odedra

Reputation: 11141

This will work, try this,

def _get_commitment_date(self, cr, uid, ids, name, arg, context=None):
    res = {}
    dates_list = []
    deliveryperiod_obj = self.pool.get('stock.deliveryperiods')
    for order in self.browse(cr, uid, ids, context=context):
        dates_list = []

        deliveryperiod_read_test = deliveryperiod_obj.read(cr, uid, 4, ['name'], context)
        deliveryperiod_browse_test = deliveryperiod_obj.browse(cr,uid,4).name 

        deliveryperiod_read = deliveryperiod_obj.read(cr, uid, [order.x_deliveryperiod.id], ['name'], context)
        deliveryperiod_browse = deliveryperiod_obj.browse(cr,uid,[order.x_deliveryperiod.id]).name 

        dt = datetime.strptime(order.date_order, '%Y-%m-%d') + relativedelta(days=deliveryperiod_read or 0.0)
        dt_s = dt.strftime('%Y-%m-%d')
        dates_list.append(dt_s)
        if dates_list:
            res[order.id] = min(dates_list)
    return res

Upvotes: 1

Related Questions