Ketika kita mengembangkan aplikasi berbasis Odoo kita mungkin akan menghubungkan satu model dengan yang lainnya. Saat ini kita akan menghubungkan model yang kita buat ke model res.users dengan relasi many2one.

Penamabahan field relasi many2one Odoo mengunakan OpenCode adalah seperti pada langkah-langkah berikut ini

  1. Backup source code modul
    erpku@odoo-test2:~/custom-addons/botolku$ cd ..
    erpku@odoo-test2:~/custom-addons$ zip -r botolku-20260508.zip botolku
      adding: botolku/ (stored 0%)
      adding: botolku/__manifest__.py (deflated 48%)
      adding: botolku/security/ (stored 0%)
      adding: botolku/security/ir.model.access.csv (deflated 44%)
      adding: botolku/models/ (stored 0%)
      adding: botolku/models/models.py (deflated 50%)
      adding: botolku/models/__init__.py (stored 0%)
      adding: botolku/models/__pycache__/ (stored 0%)
      adding: botolku/models/__pycache__/models.cpython-312.pyc (deflated 38%)
      adding: botolku/models/__pycache__/__init__.cpython-312.pyc (deflated 17%)
      adding: botolku/__init__.py (stored 0%)
      adding: botolku/views/ (stored 0%)
      adding: botolku/views/botolku_views.xml (deflated 77%)
      adding: botolku/__pycache__/ (stored 0%)
      adding: botolku/__pycache__/__init__.cpython-312.pyc (deflated 16%)
    
  2. Meminta OpenCode membuat field manyone dengan mengisi prompt
  3. Hasil kerja OpenCode pada model
  4. Hasil kerja OpenCode pada view

  5. Restart Odoo dan upgrade modul
    root@odoo-test2:~# systemctl restart odoo
    

  6. Form yang sudah diperbarui

  7. Melihat model yang diperbarui
    from odoo import models, fields
    
    class Botolku(models.Model):
        _name = "botolku.botolku"
        _description = "Data Botol"
    
        name = fields.Char(string="Nama Botol", required=True)
        kode_botol = fields.Char(string="Kode Botol", required=True, copy=False)
        panjang = fields.Float(string="Panjang (cm)")
        diameter = fields.Float(string="Diameter (cm)")
        keterangan = fields.Text(string="Keterangan")
        active = fields.Boolean(default=True)
        penjual = fields.Many2one("res.users", string="Penjual", default=lambda self: self.env.user)
    
        _sql_constraints = [
            ("kode_botol_unique", "UNIQUE(kode_botol)", "Kode botol harus unik!"),
        ]
    
  8. Melihat tabel PostgreSQL yang diperbarui
    erpku@odoo-test2:~/custom-addons/botolku$ psql -d ent16
    psql (16.13 (Ubuntu 16.13-0ubuntu0.24.04.1))
    Type "help" for help.
    
    ent16=> \d botolku_botolku;
                                             Table "public.botolku_botolku"
       Column    |            Type             | Collation | Nullable |                   Default                   
    -------------+-----------------------------+-----------+----------+---------------------------------------------
     id          | integer                     |           | not null | nextval('botolku_botolku_id_seq'::regclass)
     create_uid  | integer                     |           |          | 
     write_uid   | integer                     |           |          | 
     name        | character varying           |           | not null | 
     kode_botol  | character varying           |           | not null | 
     keterangan  | text                        |           |          | 
     create_date | timestamp without time zone |           |          | 
     write_date  | timestamp without time zone |           |          | 
     panjang     | double precision            |           |          | 
     diameter    | double precision            |           |          | 
     active      | boolean                     |           |          | 
     penjual     | integer                     |           |          | 
    Indexes:
        "botolku_botolku_pkey" PRIMARY KEY, btree (id)
    Foreign-key constraints:
        "botolku_botolku_create_uid_fkey" FOREIGN KEY (create_uid) REFERENCES res_users(id) ON DELETE SET NULL
        "botolku_botolku_penjual_fkey" FOREIGN KEY (penjual) REFERENCES res_users(id) ON DELETE SET NULL
        "botolku_botolku_write_uid_fkey" FOREIGN KEY (write_uid) REFERENCES res_users(id) ON DELETE SET NULL
    
  9. Melihat data di PostgreSQL
    erpku@odoo-test2:~/custom-addons/botolku$ psql -d ent16
    psql (16.13 (Ubuntu 16.13-0ubuntu0.24.04.1))
    Type "help" for help.
    
    ent16=> select id, name, kode_botol, penjual  from botolku_botolku;
     id |    name     | kode_botol | penjual 
    ----+-------------+------------+---------
      1 | Botol 10 cm | Botol-10   |       1
      2 | Botol 15 cm | botol-15   |       2
    (2 rows)
    
    ent16=> select id,signature from res_users where id=2;
     id |        signature         
    ----+--------------------------
      2 | <div>Administrator</div>
    (1 row)
    
    ent16=> 
    

Pembelajaran lebih lanjut mengenai artikel ini silahkan mengunjungi https://www.odoo.com/documentation/19.0/developer/tutorials/server_framework_101/07_relations.html#many2one .

Kunjungi www.proweb.co.id untuk menambah wawasan anda.

Penambahan Field Relasi Many2one Odoo Mengunakan OpenCode