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

Hello, if any one can help me. Usually when you use a many2many relation between two classes Odoo automatically create a many-to-many table.
I want to make my own customized many2many class resultant of a many2many relation between two classes, that  many2many class/table must have others fields.
I want to populate and retrieve data from that many2many class/table and fields.
I'm aware that i can make a class, and in consecuence a table with two one2many relation to other tables, and from one of that other table make a many2many relation descraibing the same name of the many2many table.


Avatar
Discard
Best Answer

Hi,
The syntax for Many2Many fields is :-

    fields.Many2many(comodel_name=None, relation=None, column1=None, column2=None, string=None)

Here comodel_name is the name of target model,
and relation is name of table that store relation in database. ie it will create a table in database with the given name, which store the relation,
column1 and column2  are the name of columns of relation-table in database.

If you are talking about to and fro relation between two objects(tables). Lets assume that our objects are teachers and subjects, and we want a to and fro relation, i.e.  we can add teachers to subject from subject-form-view and we can also add subjects to teacher from teacher-form-view, either way it have to reflect in both sides. This we can achieve using same relation-table for both Many2many fields. For more understanding, refer code

class Teacher(models.Model):
_name = "teacher.teacher"

    name = fields.Char(string='Teacher Name')
    subject_ids = fields.Many2many('subject.subject', 'teacher_sub_rel', 'subject_id', 'teacher_id', string='Teachers')

class Subject(models.Model):
_name = "subject.subject"

    name = fields.Char(string='Subject Name')
    teacher_ids = fields.Many2many('teacher.teacher', 'teacher_sub_rel', 'teacher_id', 'subject_id', string='Subject')


Hope this helps.

Avatar
Discard
Related Posts Replies Views Activity
1
Feb 24
839
0
Jul 24
1507
0
Jul 22
60
1
Jul 22
1420
2
Dec 23
31976