I know how to make a header->detail
relationship using the One2many
relationship. It is pretty simple, but in the real world we need to have more complicated things than that.
In my case I have these tables.
Student Homework Paper Test
--------------------------------------------
id id id id
name subject title score
The Student
table has
one2many
to Homework
one2many
to Paper
one2many
to Test
and also
Homework
has many2one
to Student
Paper
has many2one
to Student
Test
has many2one
to Student
Resulting in these relationships
Student Homework Paper Test
--------------------------------------------
id id id id
name subject title score
homework_ids student_id student_id student_id
paper_ids
test_ids
I could make a form with Student
model and input Homework
, Paper
, and Test
from that form using the One2many
widget.
But what if I wanted to have a form with Homework
model where I choose a Student
first and then input her Homework
, but also from that form I'd like to see(or input) her other information such as Paper
, and Test
?
Using Many2many
relation between Homework
, Paper
, and Test
just to have the student's Paper
, and Test
accessible from Homework
seems very weird to me. It also complicates the database structure, creating unnecessary Many2many
relation.
Is there another way instead of using Many2many
relation? I feel the database structure will be very ugly if I used that.
This is a huge concern to me as looking at Odoo as it is a heavily model dependent framework. You must start with the right model first!
when you have a many2one field you can create related field to show them in the current form.
Your case in homework model you can create a related field to one2many field of paper.
paper_ids = fields.One2many(related='student_id.paper_ids', readonly = True)
i'm assuming that your field is named paper_ids when you create a related field just keep the same type always in this case one2many field and you can remove readonly if you want the user to be able to edit the field from that form. related field are like compute field are not stored in database if you want to store the in database you must set store = True.
Hope this helps you