This question has been flagged
3 Replies
34675 Views

I have the next table in Odoo, named relationship, which comes from the relation between the table girl and the table boy:

girl_id
boy_id
relationship_type
1
2
friends
1
3
siblings
2
7
lovers

So:

  • In the table girl there's a field relationships which is a one2many pointing to the table relationship.

  • In the table boy there's a field relationships which is a one2many pointing to the table relationship.

  • In the table relationship there are two fields, girl_id and boy_id, both pointing to the tables girl and boy respectively.

Scenario:

In the forms of girl and boy there's the field relationships. When I add a new relationship for a girl or a boy, a form is opened to fill in the fields of the table relationship (girl_id, boy_id and relationship_type). Imagine I'm in the form of a girl, I click on add a new relationship and the form is opened. I implemented this in order not to see the girl_id (it's invisible but it contains the ID of the current girl). So I can see only two fields (boy_id and relationship_type).

What I want:

Keeping on with the example, if I open the dropdown of boy_id, I'll see all boys, even the ones who are already related to this girl, and that's what I don't want. For example, if I'm adding a relationship to the girl with id 1, I must not see boys with ids 2 and 3, if the girl is the one with id 2, I must not see the boy with id 7.

My code:

<field name="girl_id" invisible="1"/>
<field name="boy_id" domain="[('id', 'not in', girl_id.relationships.boy_id)]"/> <!-- Obviously this does not work -->
<field name="relationship_type"/>

Can anyone help me, please? Thank you!

Avatar
Discard

1) wrong domain, it should not work... 2) why do not use many2many relations between girl and boy? many2many relation seems more convenient to use in your scenario and all this packet of technical problems will disappear with it

Author

Because it's my understanding that it's not possible to store more fields than the IDs in a many2many relationship, is it?

Best Answer

you can use relation many2many...

use in girl's model:

relationships = fields.Many2many(comodel_name="boy.model.name", relation='relation_table_name', column1='boy_id', column2='girl_id', string="relationships of this girl")


use in boy's model:

relationships = fields.Many2many(comodel_name="girl.model.name", relation='relation_table_name', column1='girl_id', column2='boy_id', string="relationships of this boy")


note that in both cases relation table is the same ( relation='relation_table_name' )

but column1 and column2 are inversed (in case of girl column1='boy_id' and column2='girl_id' but in case of boy column1='girl_id' and column2='boy_id'


UPDATE:

 apart of this two models, you can add new model and force it to have same table name as the relation table (using _table field):

_table = "relation_table_name"

and add the two fields representing ids:

boy_id = fields.Many2one(comodel_name="boy.model.name", string="Boy's ID")

girl_id = fields.Many2one(comodel_name="girl.model.name", string="Girl's ID")

also additional fields you like, as:

relationship_type = fields.Selection(....         (or fields.Char(... or something else...)

... 

you can find useful the last model.


OR


you can try to correct your domain as follows:

<field name="boy_id" domain="[('id', 'not in', the_computed_one2many_field )]"/>

add "the_computed_one2many_field" computed field and add it as invisible to the view.

the 'compute' function of "the_computed_one2many_field" must return list of ids...


probably the_computed_one2many_field may be also related field to girl_id.relationships.boy_id (not computed one)

Avatar
Discard
Author

Thank you for your help @Temur, but I think that way a boy will have the same relationship with every girl and the other way around. Let's imagine that I create the relationship_type on the table boy: when I create a boy, I will have to set the relationship type of that boy? And that boy will always have that relationship type!

yes, I see. I updated the answer. it's more complicated then I thought.

Author

Thank you @Temur, I thought the last suggestion (after the OR) was the best and would work, but I get the RuntimeError: maximum recursion depth exceeded while calling a Python object, because there's a model with a one2many pointing to other model which also has a one2many pointing to the first one.

you tried to add computed field or related field?

lets say we're adding the computed field to girls model, then

@api.depends('girl_id')
def _compute_boys(self):
   #... implement the function

girls_relations = fields.One2many(comodel_name="boys.model.name", compute='_compute_boys')
for @api.depends I just guess, adapt it to your model. then use in xml:
<field name="boy_id" domain="[('id', 'not in', girls_relations )]"/>
and you'll need same for boy's relations...

probably @api.depends('relationships') and not girl_id ? I don't know your model structure