Last active
October 8, 2022 18:00
-
-
Save CakJuice/fcc3bcdb3d20457b31fdef0e56466c47 to your computer and use it in GitHub Desktop.
Example code for Many2one & One2many fields
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from odoo import models, fields | |
class ParentModel(models.Model): | |
_name = 'parent.model' | |
parent_field_1 = fields.Char(string="Parent Field 1") | |
parent_field_2 = fields.Integer(string="Parent Field 2") | |
# If you want to display child data, you must create One2many field. | |
# The One2many field used for inverse field from Many2one field | |
child_field_ids = fields.One2many('child.model', 'parent_field_id', string="Child IDS") | |
class ChildModel(models.Model): | |
_name = 'child.model' | |
child_field_1 = fields.Char(string="Child Field 1") | |
child_field_2 = fields.Integer(string="Child Field 2") | |
child_field_3 = fields.Boolean(string="Child Field 3") | |
parent_field_id = fields.Many2one('parent.model', string="Parent ID") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<odoo> | |
<record model="ir.ui.view" id="view_parent_form"> | |
<field name="name">parent.model.form</field> | |
<field name="model">parent.model</field> | |
<field name="arch" type="xml"> | |
<form> | |
<sheet> | |
<group> | |
<field name="parent_field_1"/> | |
<field name="parent_field_2"/> | |
</group> | |
<notebook> | |
<page string="Child Data"> | |
<!-- you can display child data like code below --> | |
<field name="child_field_ids"> | |
<tree> | |
<field name="child_field_1"/> | |
<field name="child_field_2"/> | |
<field name="child_field_3"/> | |
</tree> | |
</field> | |
</page> | |
</notebook> | |
</sheet> | |
</form> | |
</field> | |
</record> | |
</odoo> |
in my project I have the same relation and it's working fine
but when I delete the child record from the parent tree view (for example) it's being deleted from the database :(
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I am also looking for a similar solution, If anyone has found an answer to this please share