如何从openerp中提交的表单中访问many2one字段的值?

时间:2014-09-10 06:57:19

标签: python-2.7 openerp odoo

我在openerp中有一个带有many2one字段(x_delivery_periods)的引号。在写入数据库之前,我想使用函数字段(commitment_date)计算提交日期。所以我写下面的代码。

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."),
}

但是从self.browse oblect获取数据时会导致上述错误。任何帮助将受到高度赞赏。提前谢谢。

1 个答案:

答案 0 :(得分:1)

这将有效,试试这个,

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