Setelah kita membahas relasi Many2one pada pengembangan aplikasi Odoo 17 dengan Visual Studio Code maka kita melanjutkan dengan relasi kebalikannya yaitu One2many pada pengembangan aplikasi Odoo 17 dengan menggunakan Visual Studio Code.
Pembuatan relasi One2many pada pembuatan aplikasi Odoo 17 menggunakan Visual Studio Code adalah seperti pada penjelasan-penjelasan berikut ini
- Start SSH Filesystem client pada Windows 10
- Login SSH
- Pembuatan model asrama.student dengan relasi Many2one
from odoo import fields, models class AsramaStudent(models.Model): _name = 'asrama.student' _description = 'Informasi Asrama Student' nama = fields.Char('Nama pelajar') gender = fields.Selection( [('male','Male'),('female','Female'),('other','Other')], string='Jenis kelamin', help='Jenis kelamin pelajar', ) active = fields.Boolean('Active',default=True,help='Activate/Deactive record') room_id = fields.Many2one('asrama.room','Room',help='Pilih room') status = fields.Selection( [('draft','Draft'),('reservation','Reservation'),('pending','Pending'),('paid','Done'),('discharge','Discharge'),('cancel','Cancel')], string='Status',copy=False,default='draft', help='Status asrama student' )
- Model asrama.room dengan relasi One2many
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')
- Update __init__.py pada folder models
from . import hostel from . import asrama_room from . import asrama_student from . import asrama_amenities
- Start Odoo dengan update modul
- Tabel asrama_student
Field-field dalam tabel asrama_student:
Constrain dalam tabel asrama_student:
- Tabel asrama_room
Field-field dalam asrama_room:
Constraint pada tabel asrama_room:
- Source code pada tag notebook dan tampilan form
<?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> </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>
Kunjungi www.proweb.co.id/implementasi-odoo/ untuk menambah wawasan implementasi Odoo ERP.