域过滤器在odoo中的many2one字段? [英] domain filter for many2one fields in odoo?

查看:490
本文介绍了域过滤器在odoo中的many2one字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码是资产继承的类.在这里,我将在地点" 字段中添加"Karn/Bang/Kengeri" "karn/bang/malleshwaram" /Kengeri"将A和B添加"asset_catg_id",然后将Y和Z添加到"karn/bang/malleshwaram".

Below code is asset inherited class . Here i will add 'place' field with 'Karn/Bang/Kengeri' and 'karn/bang/malleshwaram' for 'Karn/Bang/Kengeri' will add 'asset_catg_id' with A and B. then for 'karn/bang/malleshwaram' with Y and Z.

现在在calander继承了class.如果我用'Karn/Bang/Kengeri'选择'place',则下一个字段'asset_catg_id'我只需要获得 A和B 下拉列表.如果再次'karn/bang/malleshwaram',那么我只需要获得 Y,Z 选项.并且先前选择的'asset_catg_id'值应删除.我尝试使用域过滤器选项得到键值错误

Now at calander inherited class . if i select 'place' with 'Karn/Bang/Kengeri' then next field 'asset_catg_id' i have to get only A and B drop down list. if again 'karn/bang/malleshwaram' then i have to get only Y,Z options . and previous selected 'asset_catg_id' values should get deleted . i have tried with domain filter option got keyvalue error

class asset_asset(osv.osv):
    _inherit = "asset.asset"
    #_name = "asset_asset"
    _rec_name= "folio_num"
    _columns = {
        'name': fields.char('Asset Name', size=64),
        'place': fields.many2one('asset.parentlocation', 'Location'),
        'asset_catg_id' : fields.many2one('asset.catg', 'Asset Catg Selection', select=True, required=True),}

class asset_catg(osv.Model): 
    _name="asset.catg" 
    _rec_name='name1'
    _description="Define Asset Catgs" 
    _columns={ 'name1':fields.char('Asset Catg Names',size=64,required=True),}
asset_catg()
class asset_parentlocation(osv.osv):
    _name="asset.parentlocation"
    _rec_name="location_name"
    _columns = {
        'location_name' : fields.char('Asset Location', required=True),
        'parent_location' : fields.many2one('asset.parentlocation','Parent Location'),
        'nameee':fields.many2one('ir.attachment','Attachments'),}

    def name_get(self, cr, uid, ids, context=None):
        if context is None:
            context = {}
        if not ids:
            return []
        reads = self.read(cr, uid, ids, ['location_name','parent_location'], context=context)
        res = []
        for record in reads:
            name = record['location_name']
            if record['parent_location']:
                name = record['parent_location'][1]+' / '+name
            res.append((record['id'], name))
        return res



**Following code is calendar inherited class** 


class calendar_event(osv.osv):

    _inherit = "calendar.event"
    _rec_name = 'number'

    _columns = {
            'number' : fields.char('Meeting ID',readonly=1),
            #'place' : fields.many2one('stock.location','Substation Location',),

            'place' : fields.many2one('asset.parentlocation','Substation Location',),
            #'location' : fields.selection(STATUS_SELECTION,'Location',  default='Board A'),
            'asset_catg_id' : fields.many2one('asset.catg','Asset Catg Selection', domain="[('asset_catg_id', '=',place)]"),}

推荐答案

首先,您的域在原则上是错误的.域是字段内部"的内容,换句话说,在其模型中(例如,在asset.catg模型中的字段nameid).因此,您应该先修复该问题.

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.

如果domain依赖于另一个字段,则可以使用onchange方法返回domain(使用的占位符place_id).像这样:

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

PS 这是新v8 api的示例,但是相同的原理也适用于旧api(然后您就不必使用decorator了,还要在方法上添加cr, uid, ids并通过您的视图进行调用(所有新API都不需要).看起来您仍在使用旧api进行开发.

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.

更新 对于旧的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):

<field name="place" on_change="onchange_place(place)"/>

仍然需要在asset.catg中定义一些字段,以便将其用于匹配place字段.例如:

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')

然后,当您定义资产类别时,您可以设置资产所属的位置.然后,当您选择地点calendar.event时,onchange方法将在asset_catg_id字段上正确设置域.

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.

这篇关于域过滤器在odoo中的many2one字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆