tuấn anh phạm
tuấn anh phạm

Reputation: 81

How to set defaut value for a many2one field in Odoo 9.0c?

I have customized a module that include a many2one field. Now, I would like to set the default value for that field with the codition is: [[u'bom_ids', u'!=', False]]. i have tried below code but it did not work probebly

width_id = field.Many2one('sale.order.line.width', default ='_get_width_default')

def _get_width_default(self, cr, uid, context=None): 
     res = self.pool.get('product.template').search(cr, uid, [(u'bom_ids', u'!=', False)], context=context)
     return res and res[0] or False


default = {
'width_id' : _get_width_default,
}

Could you guy please help me to point what is the problem and how to sovle the problem and finnally get my purpose. Thank for watching

Upvotes: 0

Views: 269

Answers (1)

Phillip Stack
Phillip Stack

Reputation: 3378

import logging
_logger = logging.getLogger(__name__)

def _get_width_default(self, cr, uid, context=None): 
    #REPLACE DOMAIN WITH SOMETHING RELEVANT
    domain = []
    res = self.pool.get('sale.order.line.width').search(cr, uid, domain, context=context)
    _logger.info("PREPARING DEFAULT VALUE")
    _logger.info(res)
    return res[0] or False

width_id = fields.many2one('sale.order.line.width')

_defaults = {
    'width_id' : _get_width_default,
}

What do you see as far as logging is concerned? Try the above code (v7 style).

Odoo9 Style (NEW API)

import logging
_logger = logging.getLogger(__name__)

def _get_width_default(self): 
    #REPLACE DOMAIN WITH SOMETHING RELEVANT
    domain = []
    res = self.env['sale.order.line.width'].search(domain)
    _logger.info("PREPARING DEFAULT VALUE")
    _logger.info(res)
    return res[0].id or False

width_id = fields.many2one('sale.order.line.width',default=_get_width_default)

Upvotes: 1

Related Questions