Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
2 Trả lời
8838 Lượt xem

Hi, I'm trying to figure out how to return domains for various fields in a single function, with @onchange decorator. Is this possible or I have to do cascade functions like after the first onchange function I have to use another one for the other domain? can I use 2 times the @onchange decorators? I've tried that before but without positive results.


Regards




Ảnh đại diện
Huỷ bỏ
Tác giả Câu trả lời hay nhất

@Niyas

That code seems good but when I tried in Odoo 11 didn't worked for my custom model, Also i have to return 2 or more domains. so the workaround i've used is a chain onchange-decorated functions. like 


@api.onchange(a_field)

def domainB(self)

  #build the domain string

   return domainB


@api.onchange(b_field)

def domainC(self)

  #build the domain string

  return domainC


Assuming I have to change the A and B fields to trigger the functions, I've used the form to guide the user through the proccess, making the B and C fields invisible if they don't choose a value for A field(required) first. 



Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

Hi,

I think in the onchange itself it is possible. See the function named _change_model_id in the gamification module.


@api.onchange('model_id')
def _change_model_id(self):
"""Force domain for the `field_id` and `field_date_id` fields"""
if not self.model_id:
return {'domain': {'field_id': expression.FALSE_DOMAIN, 'field_date_id': expression.FALSE_DOMAIN}}
model_fields_domain = [
('store', '=', True),
'|', ('model_id', '=', self.model_id.id),
('model_id', 'in', self.model_id.inherited_model_ids.ids)]
model_date_fields_domain = expression.AND([[('ttype', 'in', ('date', 'datetime'))], model_fields_domain])
return {'domain': {'field_id': model_fields_domain, 'field_date_id': model_date_fields_domain}}

Thanks

Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
2
thg 7 24
7851
2
thg 2 24
15591
1
thg 12 22
5020
2
thg 12 22
14498
2
thg 6 22
6360