You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
1.7 KiB

  1. Many2manyCustom field is useful when a direct access to the relational table
  2. is needed, for example to be editable in a dedicated tree view.
  3. Let's consider following models:
  4. .. code-block:: python
  5. class MyModelA(models.Model):
  6. _name = 'my.model.a'
  7. my_model_b_ids = fields.Many2manyCustom(
  8. 'my.model.b',
  9. 'my_model_a_b_rel',
  10. 'my_model_a_id',
  11. 'my_model_b_id',
  12. create_table=False,
  13. )
  14. class MyModelB(models.Model):
  15. _name = 'my.model.b'
  16. my_model_a_ids = fields.Many2manyCustom(
  17. 'my.model.a',
  18. 'my_model_a_b_rel',
  19. 'my_model_b_id',
  20. 'my_model_a_id',
  21. create_table=False,
  22. )
  23. class MyModelABRel(models.Model):
  24. _name = 'my.model.a.b.rel'
  25. my_model_a_id = fields.Many2one(
  26. 'my.model.a',
  27. required=True,
  28. index=True, # Index is mandatory here
  29. )
  30. my_model_b_id = fields.Many2one(
  31. 'my.model.b',
  32. required=True,
  33. index=True, # Index is mandatory here
  34. )
  35. By setting `create_table=False` on the Many2manyCustom field, and using the
  36. relational table name, as `_name` for the relational model, we're able to
  37. define a dedicated tree view for `my.model.a.b.rel`.
  38. .. code-block:: xml
  39. <record id="my_model_a_b_rel_tree_view" model="ir.ui.view">
  40. <field name="name">my.model.a.b.rel.tree.view</field>
  41. <field name="model">my.model.a.b.rel</field>
  42. <field name="arch" type="xml">
  43. <tree editable="top">
  44. <field name="my_model_a_id" />
  45. <field name="my_model_b_id" />
  46. </tree>
  47. </field>
  48. </record>