Odoo 的一些写法

http://blog.carllhw.com/2015/10/18/odoo-install/

在高级搜索,中隐藏字段

def fields_get(self, cr, uid, fields=None, context=None):

                    fields_to_hide = ['period_id','audit','status','stock_move_lines']

                     res =super(HrpStockMove,self).fields_get(cr, uid, fields, context)

                     for   field  in    fields_to_hide:

                                res[field]['selectable'] =False

                       return  res

新老写法转换,例1:

老写法

def move_quants_write(self, cr, uid, quants, move, location_dest_id, dest_package_id, context=None):    
            context=context or {}   
            vals = {'location_id': location_dest_id.id,'history_ids': [(4, move.id)],'reservation_id': False}
            if not context.get('entire_pack'):
                    vals.update({'package_id': dest_package_id})
            self.write(cr, SUPERUSER_ID, [q.id for q in quants], vals, context=context)

新写法

def move_quants_write(self,quants, move, location_dest_id, dest_package_id):
    context=self.env.context or {}
    vals = {'location_id': location_dest_id.id,
                'history_ids': [(4, move.id)],
                'reservation_id': False}
    if not context.get('entire_pack'):
        vals.update({'package_id': dest_package_id})
    for quant in quants:
        quant.sudo().write(vals)
    if not(move.product_id.type == 'consu' and context.get('flag')):
        if move.product_id.valuation == 'real_time':
            self._account_entry_move(quants, move)
Odoo 的一些写法_第1张图片
Paste_Image.png
  1. 有可能是之前的表被意外删除
  2. 有可能是之前继承了一个表,在后面的操作中没有继续继承。

Many2one 字段添加default 和 compute 方法。

    def _default_company_id(self):
        res = self.env['res.company'].search([])
        if len(res.ids) == 1:
            return res and res[0]
        else:
            return False

    company_id = fields.Many2one(comodel_name='res.company', string='Company Name', help='Company Name', required="1",
                                 default=_default_company_id)

动态改变domain

@api.onchange('asset_id')
def filter_asset_id(self):    
  return {'domain': {'asset_id': [('id', 'not in', self.env.context['active_ids'])]}}

你可能感兴趣的:(Odoo 的一些写法)