如何在odoo中通过many2one字段设置默认值?

时间:2016-06-15 12:53:09

标签: python xml openerp

我在'company_id1'中有以下 feeder_data 课程我将选择公司名称。该公司名称必须在 data_value 设置默认值class'company'字段。它应该选择名称作为默认值

class feeder_data(osv.Model):
    _name = "feeder.data"
    _rec_name= "company_id1"

    _columns = {
        'company_id1': fields.many2one('res.company', 'Substation', required=True),
        'date_of1':fields.datetime('Date',),
        'combine2':fields.one2many('data.value','combine','Details'),
        }
class data_value(osv.Model):
    _name = "data.value"
    _rec_name = "mega_wat"
    _columns={
        'company':field.char('Comapny'),
        'Hours':fields.integer('Folio'),
        'mega_wat':fields.float('Mega Watts'),
        'combine':fields.many2one('feeder.data','details'),
    }

data_value()

1 个答案:

答案 0 :(得分:2)

在模型feeder.data中设置 company_id1 的默认值 试试这个:

def _default_company(self, cr, uid, context=None):
    user = self.pool.get('res.users').browse(cr, uid, uid, context=context)
    if user.company_id:
        return user.company_id.id
    return self.pool.get('res.company').search(cr, uid, [('parent_id', '=', False)])[0]

_defaults = {

'company_id1': _default_company,
}