Many2One Relationship
1. we have school_profile table (module)
2. we have school_students table (module)
so school can have many student and the relationship between these to table will be one-to-many
Inside School_Students
In model define Forieng key field like so
name = fields.Char()
school_id = fields.Many2one("school.profile", string="School")
Show in XML file as separate view.
<record model="ir.ui.view" id="school_students.form">
<field name="name">school_students.form</field>
<field name="model">school.students</field>
<field name="arch" type="xml">
<form>
<group>
<group>
<field name="name" />
<field name="school_id" string="School" />
</group>
</group>
</form>
</field>
</record>
Update the module and see the changes. Now you can assign school to student while creating record.
One2Many relationships and inherit view
Define relation in model with new class
class SchoolProfile(models.Model):
_inherit = "school.profile"
school_list = fields.One2many("school.students", "school_id",
string="School List")
Inherit View in the XML file.
<record model="ir.ui.view" id="school_extend_form">
<field name="name">school_extend form</field>
<field name="model">school.profile</field>
<field name="inherit_id" ref="school.school_form_view" />
<field name="arch" type="xml">
<field name="school_description" position="after">
<field name="school_list" />
</field>
</field>
</record>
Comments
Post a Comment