Community mailing list archives

community@mail.odoo.com

Re: Filter field using another field from another model

by
dom
- 07/28/2016 01:13:25

Suggestion(no guarantee):

You could create a computed field in the parent model, that aggregates the products in the child1.
Add this field in the form view, but leave it invisible.
Then for child2, filter according this invisible field.

Let us know if that would work...


On 28 Jul, 2016 12:07 pm, "vak0160" <vak0160.work@gmail.com> wrote:

Hello Togar,

I don't think I can do that, since users would be confused since they don't need the m2o field to child1, and also the product_id doesn't need domain anymore since I can just relate it from child1 (related or computed field).

On 07/28/2016 10:57 AM, Togar Hutabarat wrote:
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


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


-- 
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