Community mailing list archives

community@mail.odoo.com

Re: Filter field using another field from another model

by
Togar Hutabarat
- 07/27/2016 23:54:33
Hi vak0160,

I don't see any possible way to get the domain for this case. However, you can do this by creating another additional field in model mymodel.child2, a many2one field, referenced to object mymodel.child1:
# class Child2 
child1_id = fields.Many2one('mymodel.child1','Child 1 Reference')
 
In your XML view, you can add the domain:
<!-- view for Child2 -->
<field name="parent_id" />
<field name="child1_id" domain=[('parent_id','=',parent_id)] />
<field name="product_id" />

Then finally you can add a simple onchange function to this field that will change product_id based on product_id chosen in current record of mymodel.child1.

Hope it would help.

Happy coding!

On Thu, Jul 28, 2016 at 10:13 AM, vak0160 <vak0160.work@gmail.com> wrote:

Hello Community,

I have this code:

class Parent(models.Model):
    _name = "mymodel.parent"

    child1_ids = fields.One2many("mymodel.child1", "parent_id")
    child2_ids = fields.One2many("mymodel.child2", "parent_id")


class Child1(models.Model):
    _name = "mymodel.child1"

    parent_id = fields.Many2one("mymodel.parent", required=True)
    product_id = fields.Many2one("product.product", required=True)


class Child2(models.Model):
    _name = "mymodel.child2"

    parent_id = fields.Many2one("mymodel.parent", required=True)
    product_id = fields.Many2one("product.product", required=True, domain="???")  # domain using `mymodel.child1` record

I wanted to filter the `product_id` field on `mymodel.child2` model, using `product_id` on `mymodel.child1` model on the same parent.


Example:

The parent have 3 record on field `child1_ids`, each have different product (product1, product2, product3), and I want the product in field `child2_ids` can only select product1, product2, or product3, not product4 or another product not set on `child1_ids`. How I can do that?

-- 
Regards,
Andre

_______________________________________________
Mailing-List: https://www.odoo.com/groups/community-59
Post to: mailto:community@mail.odoo.com
Unsubscribe: https://www.odoo.com/groups?unsubscribe