Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

4

How to set this specific domain in XML?

By
Juan Formoso
on 3/31/15, 12:08 PM 2,067 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!

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

Temur
on 3/31/15, 12:25 PM

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

Juan Formoso
on 3/31/15, 12:42 PM
0

Temur

--Temur--
2894
| 6 5 7
Tbilisi, Georgia
--Temur--
Programmer
Temur
On 3/31/15, 12:50 PM

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)

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!

Juan Formoso
on 3/31/15, 1:17 PM

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

Temur
on 3/31/15, 1:19 PM

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.

Juan Formoso
on 4/1/15, 4:02 AM

you tried to add computed field or related field?

Temur
on 4/1/15, 4:08 AM

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...
Temur
on 4/1/15, 4:44 AM

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

Temur
on 4/1/15, 4:49 AM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 3/31/15, 12:08 PM
Seen: 2067 times
Last updated: 4/23/15, 6:04 AM