Relasi Many2many pada Pengembangan Aplikasi Berbasis Odoo 17

Jan 15, 2025 | Odoo 17 Development

Dalam mengembangkan aplikasi berbasis Odoo 17 kita mungkin akan mendapatkan relasi antar tabel many2many. Di sini kita akan membahas penerapan relasi many2many pada pengembangan aplikasi berbasis Odoo 17.

Relasi many2many pada pengembangan aplikasi berbasis Odoo 17 adalah seperti pada penjelasan-penjelasan berikut ini

  1. Menjalankan SSH Filesytem
  2. Model asrama.amenities

    from odoo import fields, models
    
    class AsramaAmenities(models.Model):
        _name = 'asrama.amenities'
        _description = 'Asrama Amenities'
    
        name = fields.Char('Nama',help='Amenities yang disediakan')
        active = fields.Boolean('Active',help='Activate/Deactivated record')
    

  3. Model asrama.room

    from odoo import fields, models
    
    class AsramaRoom(models.Model):
        _name = 'asrama.room'
        _description = 'Informasi Kamar Asrama'
        _rec_name = 'room_no'
    
        name = fields.Char('Nama kamar',required=True)
        room_no = fields.Char('No kamar',required=True)
        floor_no = fields.Integer('No Lantai',default=1, help='No Lantai')
        currency_id = fields.Many2one('res.currency',string='Mata uang')
        rent_amount = fields.Monetary('Harga sewa',help='Masukkan harga sewa')
    
        hostel_id = fields.Many2one('asrama.hostel','Hostel', help='Nama hostel')    
        student_ids = fields.One2many('asrama.student','room_id','Daftar pelajar',help='Pelajar-pelajar dalam room ini')
    
        asrama_amenities_ids = fields.Many2many('asrama.amenities',
            'asrama_amenities_rel','room_id','amenity_id',
            string='Amenities', domain="[('active','=',True)]",
            help='Pilih fasilitas room' 
        )
    

  4. View asrama_room
    <?xml version='1.0' encoding='utf-8'?>
    <odoo>
        <!-- asrama.room tree view -->
        <record id="asrama_room_view_tree" model="ir.ui.view">
            <field name="name">asrama.room.view.tree</field>
            <field name="model">asrama.room</field>
            <field name="arch" type="xml">
                <tree string='Room'>
                    <field name="name"/>
                    <field name="room_no" />
                    <field name="floor_no" />
                </tree>
            </field>
        </record>
    
        <!-- asrama.room form view -->
        <record id="asrama_room_view_form" model="ir.ui.view">
            <field name="name">asrama.room.view.form</field>
            <field name="model">asrama.room</field>
            <field name="arch" type="xml">
                <form string="Room">
                    <sheet>
                        <group>
                            <group description='Kamar'>
                                <field name="name" />
                                <field name="room_no" />
                                <field name="hostel_id" required="1"/>
                                <field name="floor_no" />
                            </group>
                            <group description='Harga'>
                                <field name="rent_amount" />
                                <field name="currency_id" />
                            </group>
                        </group>
                        <notebook>
                            <page name="room_studennt" string="Para Pelajar">
                                <field name="student_ids"/>
                            </page>
                            <page name="asrama_amenities" string="Room Amenities">
                                <group>
                                    <field name="asrama_amenities_ids" widget="many2many_tags"/>
                                </group>
                            </page>
                        </notebook>
                    </sheet>
                </form>
            </field>
        </record>
    
        <!-- asrama.room action window -->
        <record id="asrama_room_action" model="ir.actions.act_window">
            <field name="name">Room</field>
            <field name="type">ir.actions.act_window</field>
            <field name="res_model">asrama.room</field>
            <field name="view_mode">tree,form</field>
            <field name="domain">[]</field>
            <field name="context">{}</field>
            <field name="help" type="html">
                <p class="oe_view_nocontent_create">
                    Tambah kamar
                </p>
            </field>
        </record>
    
        <!-- This Menu Item must have a parent and an action -->
        <menuitem id="asrama_room_menu" name="Room Asrama" parent="hostel_main_menu" action="asrama_room_action" sequence="2"/>
    
    </odoo>
    
  5. Start Odoo dengan update modul

  6. Tabel asrama_room
    Field-field pada tabel asrama_room:


    Beberapa constaint pada tabel asrama_room:

  7. Tabel asrama_amenities
    Field-field pada asrama_amenities:


    Beberapa constraint pada asrama_amenities:

  8. Tabel asrama_amenities_rel
    Field-field pada tabel asrama_amenities_rel:


    Foreign key ke tabel asrama_amenities:


    Foreign key ke tabel asrama_room:
  9. Tampilan pada Odoo
    Tampilan awal:


    Memasukkan amenity yang berelasi Many2many:

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