コンテンツへスキップ
メニュー
この質問にフラグが付けられました
7 返信
8440 ビュー
I'm having a problem with returning the domain for the field when executing onchange method.
For the code below, it works fine on odoo version 14. But on odoo version 17, it doesn't work. Can you guys give me a solution to do this?
I want the service_request_ids field to display only according to the selected service_id.

service_id = fields.Many2one('ahamove.service', string='Service')
service_request_ids = fields.Many2many('ahamove.service.request', 'estimate_request_rel','choose_id', 'request_id', string='Request Type')

@api.onchange('service_id')
def _onchange_service_id(self):
for rec in self:
if rec.service_id:
return {'domain': {'service_request_ids': [('service_id', '=', rec.service_id.id)]}}
アバター
破棄
最善の回答

Hi  Long Duong Nhat,

@api.onchange functions that return a domain have been deprecated from v17.

Check this alternative method,
https://github.com/odoo/odoo/blob/26239b2d0bdbc2f06d50f7739d61c490248b65bb/addons/account/models/account_tax.py#L1633C5-L1633C154


アバター
破棄
著作者

Hi Kiran K, thank you for your response. I will try your solution now.

著作者 最善の回答

Hi Dương Nguyễn, Kiran KCybrosys Techno Solutions Pvt.Ltd,
Thank you very much for assisting me with different solutions. 
After testing all the solutions, I solved the problem most suitably (By compiling your solutions).

class ChooseDeliveryCarrier(models.TransientModel):
_inherit = 'choose.delivery.carrier'

​service_id = fields.Many2one('ahamove.service', string='Service')
​service_request_domain = fields.Binary(default=[])
​service_request_ids = fields.Many2many('ahamove.service.request', ​'estimate_request_rel','choose_id', 'request_id', string='Request Type')

@api.onchange('service_id')
​def _onchange_service_id(self):
​for rec in self:
​if rec.service_id:
​rec.service_request_domain = [('service_id', '=', rec.service_id.id)]

widget="many2many_tags"
options="{'no_create': True}"
invisible="delivery_type != 'ahamove'"
domain="service_request_domain"
/>

Best regards,
Long Duong Nhat


アバター
破棄
最善の回答

Hi,


You can use the compute function instead like this:

service_id = fields.Many2one('ahamove.service', string='Service')

service_request_ids = fields.Many2many('ahamove.service.request', 'estimate_request_rel',

'choose_id', 'request_id', string='Request Type',                       compute='_compute_service_request_ids', store=True)

@api.depends('service_id')

def _compute_service_request_ids(self):

for rec in self:

if rec.service_id:

rec.service_request_ids = self.env['ahamove.service.request'].search([('service_id', '=', rec.service_id.id)])


Hope it helps


アバター
破棄
著作者

Hi Cybrosys, thank you for the solution. It seems to work, the service_request_ids are all loaded according to the correct service_id. However, service_request_ids is not selectable (it's read-only).

最善の回答

Does adding domain in field definition not work ?

service_request_ids = fields.Many2many(domain="[('service_id', '=', service_id)]")

This assume that service_request_ids containt service_id in it as m2o field

アバター
破棄
著作者

Hi Duong Nguyen, thank you for your response.
It (domain="[('service_id', '=', service_id)]") doesn't work. When I select it, it cannot load the record

著作者

The relationship between service_id and service_request_ids are One2Many.
class AhamoveService(models.Model):
_name = 'ahamove.service'
request_ids = fields.One2many('ahamove.service.request', 'service_id', 'Requests')

class AhamoveServiceRequest(models.Model):
_name = 'ahamove.service.request'
service_id = fields.Many2one('ahamove.service', string='Service')

Which mean when user choose service_id on form view it will automatically load all related record of 'ahamove.service.request' which have that 'service_id'
If that so, i suggest using compute store with readonly=False

著作者

Yes, that's right,
But as far as I understand,
domain="[('service_id', '=', service_id)]" and compute method is a form of the static domain. This means that every time you access the form, it will work as expected. But when you onchange service_id it will not work. I have tried doing this.

No, i haven't seen any one implement onchange method like you did.
Normally , when implement onchange , it will be used to change some other field value and the most important thing of onchange is not store on the database level.
So i guest u should remove your onchange method and do something like this
@api.depends('service_id')
def _compute_request_ids(self):
all_ahamove_service_request = self.env["ahamove.service.request"].search([("service_id", "in", self.service_id.ids)])
for r in self:
if r.service_id:
r.request_ids = all_ahamove_service_request.filtered(lambda req: req.service_id == r.service_id )
else:
r.request_ids = False

著作者

Thanks Duong, I will try your solution now.

最善の回答

Hi, you can follow this: https://youtu.be/f3mq5fHRPT0

Hope it helps, Thanks

アバター
破棄
著作者

Thanks so much, Adil Akbar

最善の回答

Why are you using binary field to store domain and do you think in xml it will render it in correct format and work? I have not tried the solution but I am curious about why take a binary field?

​service_request_domain = fields.Binary(default=[])
アバター
破棄
最善の回答

Hi Long,
Thank you so much. I've been searching for days on how to implement this in Odoo 17.
I only added this to my "service_requests_ids" field in the XML view:

widget="many2many_tags"
options="{'no_create': True}"
domain="service_request_domain"

It is important to note that the "service_request_domain" must be included in the XML view file and set to invisible.

Best regards,
Nicolás

アバター
破棄
著作者

Hi Nicolás,
#python-code
class ChooseDeliveryCarrier(models.TransientModel):
_inherit = 'choose.delivery.carrier'

​service_id = fields.Many2one('ahamove.service', string='Service')
​service_request_domain = fields.Binary(default=[])
​service_request_ids = fields.Many2many('ahamove.service.request', ​'estimate_request_rel','choose_id', 'request_id', string='Request Type')

@api.onchange('service_id')
​def _onchange_service_id(self):
​ ​for rec in self:
​ ​if rec.service_id:
​ ​rec.service_request_domain = [('service_id', '=', rec.service_id.id)]

#xml-code
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<record id="view_choose_delivery_carrier_form_with_provider" model="ir.ui.view">
<field name="name">view.choose.delivery.carrier.form.with.provider</field>
<field name="model">choose.delivery.carrier</field>
<field name="inherit_id" ref="delivery.choose_delivery_carrier_view_form" />
<field name="arch" type="xml">
<field name="carrier_id" position="after">
<field name="service_id"/>
<field name="service_request_ids" widget="many2many_tags" domain="service_request_domain"/>
<field name="service_request_domain" invisible="1"/>
</field>
</field>
</record>
</odoo>

Thanks and best regards,

関連投稿 返信 ビュー 活動
1
9月 19
3926
3
5月 24
4662
3
11月 24
44837
1
4月 23
4753
7
4月 23
18709