domain filter for many2one fields in odoo?

前端 未结 1 1904
攒了一身酷
攒了一身酷 2021-01-06 12:31

Below code is asset inherited class . Here i will add \'place\' field with \'Karn/Bang/Kengeri\' and \'karn/bang/malleshwaram\'

1条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-06 13:05

    First your domain is wrong in a principle. Domain is what is "inside" a field, in other words in its model (for example field name or id in asset.catg model). So you should fix that one first.

    If domain depends on another field, you can use onchange method to return domain (used placeholder place_id). Like this:

    @api.onchange('place')
    def onchange_place(self):
        res = {}
        if self.place:
            res['domain'] = {'asset_catg_id': [('place_id', '=', self.place.id)]}
        return res
    

    P.S. This is example with new v8 api, but same principle applies to old api (you then don't nee to use decorator, also add cr, uid, ids on method and call it through your view. All of this not needed for new api). As it looks like you are still developing on old api.

    Update For old api:

    def onchange_place(self,cr, uid, ids, place, context=None):
        res = {}
        if self.place: #on old api it will return id, instead of record
            res['domain'] = {'asset_catg_id': [('place_id', '=', self.place)]}
        return res
    

    And then in your view (don't know what kind of view you are using):

    
    

    Still you need to define some field in asset.catg so it would be used to match place field. For example:

    'place_id': fields.many2one('asset.parentlocation', 'Place')
    

    And then when you define asset category, you set which Place it should belong to. Then when you choose place calendar.event, onchange method will set domain on asset_catg_id field correctly.

    0 讨论(0)
提交回复
热议问题