Odoo is the world's easiest all-in-one management software. It includes hundreds of business apps:
CRM | e-Commerce | Accounting | Inventory | PoS | Project management | MRP | etc.
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 |
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.
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.
<field name="girl_id" invisible="1"/>
<field name="boy_id" domain="[('id', 'not in', girl_id.relationships.boy_id)]"/> <!-- Obviously this does not work -->
Can anyone help me, please? Thank you!
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'
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.
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)
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 platform 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.Register
Odoo Training Center
Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.Test it now
|Asked: 3/31/15, 12:08 PM|
|Seen: 2402 times|
|Last updated: 4/23/15, 6:04 AM|