Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

filter field based on the value from other fields

By
Khalid Masood
on 1/12/14, 1:48 PM 2,438 views

Hi,

I have three fields in three different classes which are related in a chain: District, Sub District, Area

Area is linked to Sub District, Sub District is Linked to District.

class sna_district(osv.osv): _name="sna.district" _description="Define districts" _columns={ 'name':fields.char('District Name',size=64,required=True) }

class sna_sub_district(osv.osv): _name="sna.sub.district" _description="Define sub districts" _columns={ 'name':fields.char('Sub District Name',size=64,required=True), 'sub_district_id': fields.many2one('sna.district','District Name',select=True,required=True,ondelete="restrict") }

class sna_area(osv.osv): _name="sna.area" _description="Define areas" _columns={ 'name':fields.char('Area Name',size=64,required=True),
'area_type': fields.selection(area_type_lov,'Area Type',required=True), 'sub_district_id':fields.many2one('sna.sub.district','Sub District Name',size=64,required=True), 'district_id':fields.related('sub_district_id','sub_district_id',readonly=True,type='many2one',relation='sna.district',string='District Name'),
}

Above three are working fine.

In another class, user need to specify all three fields, user selects the 'District' in the first field, based on the 'District' selected, system should display the related 'Sub Districts' only in the second field (the ones which are linked to selected 'District'), based on the 'Sub District' selected by the user (from filtered list), system should display the related 'Area' only in the third field (the ones which are linked to selected 'Sub District').

Below is what I am trying but it looks wrong and therefore not working:

class sna_aor_super(osv.osv): _name="sna.aor.super" _description="Area of Registration Super" _columns={ 'district_id': fields.many2one('sna.district','District',select=True,required=True,ondelete="restrict"), 'sub_district_id':fields.related('district_id','district_id',readonly=True,type='many2one',relation='sna.sub.district',string='Sub District'), 'area_id':fields.related('sub_district_id','sub_district_id',readonly=True,type='many2one',relation='sna.area',string='Area'), }

Help will be much appreciated.

Thanks

1
Petar
On 1/12/14, 7:25 PM

Hi Khalid,

If you want to apply that kind of filtering you should use domains on fields instead of using related fields

class sna_aor_super(orm.Model): 
    _name = "sna.aor.super" 
    _description = "Area of Registration Super"
    _columns = { 
        'district_id' : fields.many2one('sna.district','District', select=True, required=True, ondelete="restrict"), 
        'sub_district_id' : fields.many2one('sna.sub.district', 'Sub District', domain="[('sub_district_id', '=', district_id)]", select=True, required=True),
        'area_id' : fields.many2one('sna.area', 'Area', domain="[('sub_district_id', '=', sub_district_id)]", select=True, required=True),
    }

Regards, Petar

Thank you for the help. Much appreciated.

Khalid Masood
on 1/14/14, 4:10 PM

You are very welcome Khalid :)

Petar
on 1/16/14, 11:19 AM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 1/12/14, 1:48 PM
Seen: 2438 times
Last updated: 3/16/15, 8:10 AM