Skip to content

Instantly share code, notes, and snippets.

@repodevs
Created September 6, 2017 05:48
Show Gist options
  • Save repodevs/e77ec4391c3059a0f0639bc53de76cf0 to your computer and use it in GitHub Desktop.
Save repodevs/e77ec4391c3059a0f0639bc53de76cf0 to your computer and use it in GitHub Desktop.
odoo name_get inherit
# -*- coding: utf-8 -*-
# © 2017 Niaga Solution - Edi Santoso <[email protected]>
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).
from odoo import api, models
class Contact(models.Model):
_inherit = 'res.partner'
@api.multi
def name_get(self):
res = super(Contact, self).name_get()
res_dict = dict(res)
for record in self:
if record.unit_id:
res_dict[record.id] = "(%s) %s" % (record.unit_id.code, record.name)
return res_dict.items()
@mhdsyarif
Copy link

Kalau kasusnya seperti ini mas.

class management(models.Model):
    _name = 'management.activity'
    pic = fields.Many2one('res.partner', string="PIC", required=True, 
        track_visibility='onchange')

Saya ingin nanti field pic pada saat dropdown muncul dua field.
Penulisan API gimana ya mas

@api.multi
def name_get(self):
    res = super(Contact, self).name_get()
    res_dict = dict(res)
    for record in self:
        if record.unit_id:
            res_dict[record.id] = "(%s) %s" % (record.unit_id.code, record.name)
    return res_dict.items()

@repodevs
Copy link
Author

repodevs commented Sep 6, 2017

@mhdsyarif 2 field yang akan di munculkan apa aja itu mas?

field pic relation ke res.partner berarti yang di inherit name_get nya res.partner mas, bukan management.activity nya

@mhdsyarif
Copy link

mhdsyarif commented Sep 6, 2017

saya ingin menampilkan name sama email

classnya sebenarnya saya potong mas, jadi saya buat tabel baru bernama management.activity di dalamnya ada field pic, jadi pada saat di form dropdown pic muncul nama dan email
saya baru bisa seperti ini.

selection_258

@repodevs
Copy link
Author

repodevs commented Sep 6, 2017

@mhdsyarif coba ini mas

from odoo import api, models

class Contact(models.Model):
    _inherit = 'res.partner'
     
    @api.multi
    def name_get(self):
        res = super(Contact, self).name_get()
        res_dict = dict(res)
        for record in self:
            if record.email:
                res_dict[record.id] = "%s / %s" % (record.name, record.email)
        return res_dict.items()

ini akan menampilkan
nama -> Jikas User tidak mempunyai e-mail
nama / [email protected] -> Jika user punya e-mail

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment