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

Good night! 

In the "Contacts" module, when we select the Country field, states are automatically filtered according to the selected country. I want to use this same feature in another situation but I can't understand how this feature was created. Everything seems to have to do with the context, but I've tried it and it didn't work, the auto filter doesn't happen. Can you help me with this conceptual doubt? 

Note: I don't really want to do anything with the Country and State fields, but if I understand how this filter occurs, I can apply it to my model, with me it would be something like selecting a car manufacturer and filtering the models in the next field. 


Thanks!


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

The way it works in the country/state example is by using an onchange function that returns a domain:

@api.onchange('country_id')
def _onchange_country_id_wrapper(self):
    res = {'domain': {'state_id': []}}
    if self.country_id:
        res['domain']['state_id'] = [('country_id', '=', self.country_id.id)]
    return res

The benefit of the onchange approach is that it allows you to select a state without a country. But it also means if you select a country, save the record, edit again and select state, it will no longer be filtered to the correct country.

If you'd prefer that the country must be selected, you can add a domain on the state field in xml like so:

 <field name="state_id" domain=[('country_id', '=', country_id)]"/>
Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

Context should work, or you can use domain, which may be a little simpler:

<field name="product_id"
    domain="[('sale_ok','=',True),('x_studio_customer','=',parent.partner_id)]" /> 

This might help: https://www.odoo.com/documentation/11.0/reference/orm.html#domains

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

Thank you so much guys!

It worked very well with me!

Thankful !!!

Ảnh đại diện
Huỷ bỏ