Skip to Content
Menu
This question has been flagged
2 Replies
2225 Views

I'm facing some problem with Many2one field when pass domain attributes in view.

This is my model 

class Product(models.Model):
_name = 'product'
store_id = fields.Many2one('store', string='Store')


class Store(models.Model):
_name = 'store'
product_ids = fields.One2many('product', 'store_id')


class Order(models.Model):
_name = 'order'
store_id = fields.Many2one('store', string='Store')
order_line = fields.One2many('order.line', 'order_id')


class OrderLine(models.Model):
_name = 'order.line'
order_id = fields.Many2one('order', string='Order')
product_id = fields.Many2one('product', string='Product')
number = fields.Integer(string='number')

This is my view

< record id="order_form" model="ir.ui.view">
< field name="name">
order.form
< field name="model">
order
< field name="arch" type="xml">
< form string="Property">
< field name="store_id" widget="Many2one"/>
< field name="order_line" widget="one2Many">
< tree editable="bottom">
< field name="product_id"
​widget="many2one"
​domain="[('store_id', '=', 'order.store_id')]"/>
< field name="number"/>
< /tree>
< /field>
< /form>
< /field>
< /record>

What I want is when adding order line in order form,
the product that can be selected is the product which has same store as the store field in order form.
But as my code, the only product I can get is nothing from the drop down selection.
Didn’t know which part get wrong.
Can anyone help please.
Thank a lot.
Avatar
Discard
Best Answer

It looks like the issue is with the domain attribute in the product_id field of the order_line view. The current domain is [('store_id', '=', 'order.store_id')], which is checking for products where the store_id field is equal to the string "order.store_id".

Instead, you should use the store_id field of the parent order record as the value for the domain. You can access the parent record's fields using the parent object. So the correct domain would be [('store_id', '=', parent.store_id)].

Also make sure that there is no typo error while refering the field in domain.

So the updated view should look like this:

order.form order

Avatar
Discard
Best Answer

Hi,

Please update the domain as follows:-

<field name="product_id"
    widget="many2one"
    domain="[('store_id', '=', parent.store_id)]"/>

Regards

Avatar
Discard