Skip to Content
Menu
Musisz się zarejestrować, aby móc wchodzić w interakcje z tą społecznością.
To pytanie dostało ostrzeżenie
2 Odpowiedzi
6555 Widoki
class Target(models.Model):
_name = "target"
product_ids = fields.Many2many('product.product', string="Products")
targetpoints_ids = fields.One2many('target.points', 'target_id', string="Points")


class TargetPoints(models.Model):
_name = "target.points"
target_id = fields.Many2one('target', required=True, index=True)
product_id = fields.Many2one('product.product', string="Product")
points = fields.Integer(string="Amount of points")

Every Target has 0, 1 or more products linked (product_ids). On the formview of Target I am also displaying the targetpoints_ids records.
- So far so good -

However I want to limit the product_id of TargetPoints to the product_ids that are previously linked to target.I thought it would be as simple as adding something like the below to the product_id field.

domain="[('id','in', target_id.product_ids.ids)]"

However nothing seems to work.
I have tried to use:
- Related fields to get the information of the other model like that (no difference)
- Put the domain filter on the view instead of in the model (no difference)
- Onchange method on product_ids that returns a domain dictionary (but can you change the domain of a field in a different model with this?)

If I literally use a list of ids [1500, 1501, 1502] instead of the .ids in my domain filter my attempts seem to do exactly what I want.

I am using Odoo 10. I am not sure if my question has to do with the dicussion on this link https://github.com/odoo/odoo/issues/16072

Any help is really appreciated.

Awatar
Odrzuć
Najlepsza odpowiedź

Hi,

Other than returning domain in onchange method, you can use the web domain field module from oca for the same:  Web Domain Field


See this: Return Dynamic Domain For Field In Odoo


How to use:

.. code-block:: xml

<field name="product_id_domain" invisible="1"/>
<field name="product_id" domain="product_id_domain"/>


.. code-block:: python

product_id_domain = fields.Char(
compute="_compute_product_id_domain",
readonly=True,
store=False,
)

@api.multi
@api.depends('name')
def _compute_product_id_domain(self):
for rec in self:
rec.product_id_domain = json.dumps(
[('type', '=', 'product'), ('name', 'like', rec.name)]
)

Returning domain from the onchange function: How To Give Domain For A Field Based On Another Field

Thanks

Awatar
Odrzuć
Najlepsza odpowiedź

You can follow this: https://youtu.be/XGqXEL2qQmE 

Hope it helps, Thanks

Awatar
Odrzuć
Powiązane posty Odpowiedzi Widoki Czynność
2
gru 24
6813
2
lis 24
27172
3
mar 24
5920
0
mar 24
967
3
lut 24
12242