Pembuatan Model pada Pemrograman Odoo 17

Jun 27, 2024 | Odoo 17 Development

Dalam pemrograman metode MVC ( Model, View , Controller)  kita mengenal model sebagai tempat menempatkan proses bisnis dan juga tabel database. Begitu juga dalam pemrograman berbasis Odoo 17 kita akan membuat model sebagai tempat menyimpan definisi tabel.

Pembuatan model pada pemrograman dengan Odoo 17 adalah seperti pada langkah-langkah berikut ini

  1. Membuat file untuk model

    Catatan gambar:
    folder asrama direname ke asramaku
    Kita masuk ke PyCharm membuat file baru pada folder models.


    Kita kemudian menamai file tersebut kemudianklik Python file
  2. Mengedit model

    Kita melengkapi pembuatan program dengan source seperti gambar di atas atau seperti di bawah ini

    from odoo import fields, models
    class Hostel(models.Model):
        _name = 'asrama.hostel'
        _description = "Informasi tentang asrama-hostel"
        name = fields.Char(string="Nama Hostel",required=True)
        hostel_code = fields.Char(string="Kode",required=True)
        street = fields.Char('Jalan')
        street2 = fields.Char('Jalan2')
        state_id = fields.Many2one('res.country.state',string="State")
    
  3. Model – init

    from . import hostel
    
  4. Modul init

    from . import models
    from . import controllers
    from . import wizards
    
  5. Perintah chmod
    $ chmod -R  a+rx asramaku
    
  6. Restart Odoo
    # docker restart odooku-web-1
    
  7. Activate developer mode

  8. Update app list

  9. Activate modul

  10. Melihat model yang terbentuk

    Dari aplikasi Settings kita menuju menu Technical | Models seperti pada gambar di atas.


    Kita kemudian membuat filter yang berkaitan dengan model yang baru saja kita buat.



    Tampak field-field yang dibuat oleh model yang kita buat.
  11. Masuk ke container database
    # docker exec -it odooku-db-1 bash
    root@f28c3368476e:/# psql -U odoo -d rnd.metiu.co.id
    psql (15.6 (Debian 15.6-1.pgdg120+2))
    Type "help" for help.
    
    rnd.metiu.co.id=#
    
  12. Melihat semua tabel
    rnd.metiu.co.id=# \dt;
    
                                  List of relations
     Schema |                        Name                         | Type  | Owner
    --------+-----------------------------------------------------+-------+-------
     public | asrama_hostel                                       | table | odoo
     public | auth_totp_device                                    | table | odoo
     public | auth_totp_wizard                                    | table | odoo
     public | base_document_layout                                | table | odoo
     public | base_enable_profiling_wizard                        | table | odoo
     public | base_import_import                                  | table | odoo
     public | base_import_mapping                                 | table | odoo
     public | base_import_module                                  | table | odoo
     public | base_language_export                                | table | odoo
     public | base_language_import                                | table | odoo
     public | base_language_install                               | table | odoo
     public | base_module_uninstall                               | table | odoo
     public | base_module_update                                  | table | odoo
     public | base_module_upgrade                                 | table | odoo
     public | base_partner_merge_automatic_wizard                 | table | odoo
     public | base_partner_merge_automatic_wizard_res_partner_rel | table | odoo
     public | base_partner_merge_line                             | table | odoo
     public | bus_bus                                             | table | odoo
     public | bus_presence                                        | table | odoo
     public | change_password_own                                 | table | odoo
     public | change_password_user                                | table | odoo
     public | change_password_wizard                              | table | odoo
     public | decimal_precision                                   | table | odoo
     public | iap_account                                         | table | odoo
     public | iap_account_info                                    | table | odoo
     public | iap_account_res_company_rel                         | table | odoo
     public | ir_act_client                                       | table | odoo
     public | ir_act_report_xml                                   | table | odoo
     public | ir_act_server                                       | table | odoo
     public | ir_act_server_group_rel                             | table | odoo
     public | ir_act_server_webhook_field_rel                     | table | odoo
     public | ir_act_url                                          | table | odoo
     public | ir_act_window                                       | table | odoo
     public | ir_act_window_group_rel                             | table | odoo
     public | ir_act_window_view                                  | table | odoo
     public | ir_actions                                          | table | odoo
     public | ir_actions_todo                                     | table | odoo
     public | ir_asset                                            | table | odoo
     public | ir_attachment                                       | table | odoo
     public | ir_config_parameter                                 | table | odoo
    
  13. Melihat tabel yang baru saja dibuat
    rnd.metiu.co.id=# \d asrama_hostel;
                                             Table "public.asrama_hostel"
       Column    |            Type             | Collation | Nullable |                  Default
    -------------+-----------------------------+-----------+----------+-------------------------------------------
     id          | integer                     |           | not null | nextval('asrama_hostel_id_seq'::regclass)
     state_id    | integer                     |           |          |
     create_uid  | integer                     |           |          |
     write_uid   | integer                     |           |          |
     name        | character varying           |           | not null |
     hostel_code | character varying           |           | not null |
     street      | character varying           |           |          |
     street2     | character varying           |           |          |
     create_date | timestamp without time zone |           |          |
     write_date  | timestamp without time zone |           |          |
    Indexes:
        "asrama_hostel_pkey" PRIMARY KEY, btree (id)
    Foreign-key constraints:
        "asrama_hostel_create_uid_fkey" FOREIGN KEY (create_uid) REFERENCES res_users(id) ON DELETE SET NULL
        "asrama_hostel_state_id_fkey" FOREIGN KEY (state_id) REFERENCES res_country_state(id) ON DELETE SET NULL
        "asrama_hostel_write_uid_fkey" FOREIGN KEY (write_uid) REFERENCES res_users(id) ON DELETE SET NULL
    
    
  14. Catatan gambar: folder asrama direname ke asramaku

Informasi lebih lanjut silahkan mengunjungi
1. https://www.odoo.com/documentation/17.0/developer/tutorials/server_framework_101/01_architecture.html .
2. https://www.odoo.com/documentation/17.0/developer/reference/backend/orm.html .
3. https://www.postgresqltutorial.com/postgresql-administration/postgresql-show-tables/ .
4. https://www3.ntu.edu.sg/home/ehchua/programming/sql/PostgreSQL_GetStarted.html .

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