Skip to Content
Menu
This question has been flagged
1 Reply
3100 Views

Hi,

I have 2 models A and B.
A is related to B like 

class B(models.Model):
A_id = fields.Many2one(comodel_name='A', inverse_name=id)

So A have many B.
On the each A arch views i want to display all B records (same order and order_lines)

How to do this in the PY and in the XML.
All example if found are very difficult to reproduce.

For now i put this in the PY

all_my_B_records = self.env['B'].search([('B_id', '=', id)])

I don't know at all if it is a good start :)

Thanks for your help,
Sebastian

Avatar
Discard
Best Answer

for the beginning, create an oposite inversed relation field (in your case, for Many2one field it'll be field of type One2many), and use that field in inverse_name argument instead of id, so your code will become as follows: 

class A(models.Model):
B_id = fields.One2many(comodel_name='B', inverse_name='A_id')


class B(models.Model):
A_id = fields.Many2one(comodel_name='A', inverse_name='B_id')

now, as both models are connected with inversed relations, you can simply add the related field to the view for class A in XML.

...
<field name="B_id" />
...

it'll display corresponding list using one2many widget.

Avatar
Discard
Author

Thank you!!!!!