Model Hirarki pada Pemrograman Odoo 17

Jan 19, 2025 | Odoo 17 Development

Model hirarki pada development pada Odoo 17 dimengerti sebagai adanya relasi many2one pada model yang sama.

Model Hirarki pada Pemrograman Odoo 17 adalah seperti pada langkah-langkah berikut ini

  1. Start SSH file system

  2. Model asrama.category

    from odoo import fields, models, api
    from odoo.exceptions import ValidationError
    
    class AsramaCategory(models.Model):
        _name = 'asrama.category'
        _description = 'Kategori Hostel'
        _parent_store = True
        _parent_name = 'parent_id'
    
        name = fields.Char('Kategori')
        parent_id = fields.Many2one(
            'asrama.category',
            string='Parent Category',
            ondelete='restrict',
            index=True)
        parent_path = fields.Char(index=True,unaccent=False)
        child_ids = fields.One2many('asrama.category','parent_id', string='Child Categories')
    
        @api.constrains('parent_id')
        def _check_hierarchy(self):
            if not self._check_recursion():
                raise models.ValidationError('Error! Tidak boleh kategori yang rekursif')
    
  3. Model asrama.hostel

    from odoo import api, fields, models
    
    class Hostel(models.Model):
        _name = 'asrama.hostel'
        _description = 'Informasi tentang asrama-hostel'
        _order = 'id desc, name'
        _rec_name = 'hostel_code'
    
        name = fields.Char(string='Nama Hostel',required=True)
        hostel_code = fields.Char(string='Kode',required=True)
        street = fields.Char(string='Jalan')
        street2 = fields.Char(string='Jalan2')
        zip = fields.Char('Zip',change_default=True)
        city = fields.Char('Kota')
        state_id = fields.Many2one('res.country.state', string='State')
        country_id = fields.Many2one('res.country', string='Negara')
        phone = fields.Char('Phone',required=True)
        mobile = fields.Char('Mobile',required=True)
        email = fields.Char('Email')
    
        hostel_floors = fields.Integer(string='Jml Lantai')
        image = fields.Binary("Gambar Hostel")
        active = fields.Boolean("Active",default=True, help="Aktifkan/Nonaktifkan data hostel")
        type = fields.Selection([('male','Laki-laki'),('female','Perempuan'),('common','Umum')]
            , 'Tipe', help='Tipe Hostel',required=True, default='common'
        )
    
        other_info = fields.Text('Informasi lain',help='Masukkan informasi lain')
        description = fields.Html('Description')
        hostel_rating = fields.Float('Rata-rata rating hostel',digits='Nilai Rating')
    
        category_id = fields.Many2one('asrama.category')   
    
        @api.depends('hostel_code')
        def _compute_display_name(self):
            for record in self:
                name = record.name
                if record.hostel_code:
                    name = f'{name} ({record.hostel_code})'
                record.display_name = name
    
  4. Init pada folder models

    from . import hostel
    from . import asrama_room
    from . import asrama_student
    from . import asrama_amenities
    from . import asrama_categ
    
  5. Start Odoo dengan update modul

  6. Tabel asrama_category

Kunjungi www.proweb.co.id/implementasi-odoo/ untuk menambah wawasan implementasi Odoo ERP.