Search code examples
odooodoo-12

Selection Filtering By Another Selection


I'm looking for a way to filter subsequent selections depending on which ones of them are filled. I want to have a tree-like structure for setting locations as such:

=>Country

==>State

===> District

So say you enter the district first, you'd only have one choice in the other two. Say you enter the country first, you'd have a more limited selection of the latter two.

I know i should be going with many2one fields but all i can get working at this point is multiple selections that turn visible/invisible which is highly inefficient.

I'm fairly new to Odoo and am finding the docs somewhat lacking. Any help would be appreciated!


Solution

  • I would play around with many2one fields and domains using the operator =?

    For example:

    
    class MyModel(models.Model):
        _name = "my.model"
    
        country_id = fields.Many2one(comodel_name="res.country")
        state_id = fields.Many2one(comodel_name="res.country.state")
        disctrict_id = fields.Many2one(comodel_name="res.country.state.district")
    

    I don't know if the model names are correct, but you should already know them.

    And in the view use the domain with the mentioned operator:

    <field name="country_id"
        domain="[('state_ids', '=?', state_id),('state_ids.district_ids', '=?', district_id)]" />
    <field name="state_id"
        domain="[('country_id', '=?', country_id),('district_ids', '=?', district_id)]" />
    <field name="disctrict_id"
        domain="[('state_id', '=?', state_id),('state_ids.country_id', '=?', country_id)]" />
    

    I didn't make any tests on those AND conditions/domains. And i don't know the structure of those 3 models. My assumption is, that there is a one2many or many2many relation on res.country -> res.country.state and res.country.state -> res.country.state.district.