Catatan Singkat Teknikal (Beberapa Syntax Yang Sering Dipakai)

1. File .py

override method create

class ms_res_partner(models.Model):
    _inherit = 'res.partner'
    
    ...
 
    @api.model
    def create(self, vals):
        vals['comment'] = "ini adalah notes"
        return super(ms_res_partner, self).create(vals)

override method write

class ms_res_partner(models.Model):
    _inherit = 'res.partner'
    
    ...
 
    @api.multi
    def write(self, vals):
        vals['comment'] = "ini adalah notes"
        return super(ms_res_partner, self).write(vals)

menggunakan digit precision

import odoo.addons.decimal_precision as dp

...

amount_total = fields.Float(string='Total Harga', compute='_get_tot_line', readonly=True, digits=dp.get_precision('Product Price'))

override method unlink

@api.multi
def unlink(self):
    for me_id in self :
        if me_id.state != 'draft' :
            raise Warning("Tidak bisa delete transaksi !")
    return super(ms_bayar_resep, self).unlink()

override method name_get

@api.multi
def name_get(self):
    result = []
    for me_id in self :
        result.append((me_id.id, "%s [%s]"%(me_id.name, me_id.partner_id.name)))
    return result

override method name_search

@api.model
def name_search(self, name, args=None, operator='ilike', limit=100):
    args = args or []
    if name :
        recs = self.search([
            '|',
            ('name', operator, name),
            ('partner_id.name', operator, name)
        ] + args, limit=limit)
    else :
        recs = self.search([] + args, limit=limit)
    return recs.name_get()

menambah value baru field Selection ketika inherit

field_name = fields.Selection(selection_add=[
    ('value1','Value 1'),
    ('value2','Value 2')
])

memanggil action report

@api.multi
def nota_penerimaan_barang(self):
    return self.env["report"].get_action(self, 'stock.report_deliveryslip')

override method default_get

@api.model
def default_get(self, fields):
    res = super(product_product, self).default_get(fields)
    if 'material' in self._context :
        res['type'] = 'product'
    return res

sql constraint

_sql_constraints = [
    ('unique_name', 'unique(name)', 'Nama duplicate !'),
]

return action

picking_ids = self.env['stock.picking'].search([])
action = self.env.ref('stock.action_picking_tree_all')
return {
    'name': action.name,
    'help': action.help,
    'type': action.type,
    'view_type': action.view_type,
    'view_mode': action.view_mode,
    'target': action.target,
    'res_model': action.res_model,
    'domain': [('id', 'in', picking_ids.ids)],
}

2. File .xml

menggunakan attrs

<field name="parent_id"/>
<field name="category_id" attrs="{'required': [('parent_id','=',False)]}"/>
<field name="code" attrs="{'required': [('parent_id','=',False)], 'readonly': [('parent_id','!=',False)]}"/>

set color pada tree

<tree string="Analytic Line" editable="top" colors="darkmagenta:has_child==True; blue:state=='draft'">
    <field name="state"/>
    <field name="has_child"/>
</tree>

memindahkan posisi fields

<field name="date_invoice" position="after">
<field name="date" position="move"/>
</field>

Insya Allah syntax yang lain akan ditambahkan…

Terimakasih, semoga bermanfaat dan CMIIW…

Tutorial odoo/openerp Indonesia