如何在选择字段上添加条件

时间:2016-05-07 00:28:09

标签: python python-2.7 openerp odoo-8 openerp-7

我是odoo的初学者,我知道如何在字段上做出选择条件,这是我的代码:

    ETAT_DOSSIER = [
    ('dos_comp','Dossier Complet'),
    ('dos_manq','Dossier Manquant'),
    ]

    def set_etat_dossier(self, cr, uid, ids, context=None):
        val = {}
        for prod in self.browse(cr, uid, ids, context=context):
            if prod.casier_judiciare ==True:
               val['etat_dos'] = ETAT_DOSSIER[0][0]
            else:
               val['etat_dos'] = ETAT_DOSSIER[1][0]

    return val

    columns= {
    'etat_dos': fields.selection(ETAT_DOSSIER,string='Etat Dossier')
              }

    defaults = {
    'etat_dos' : ETAT_DOSSIER[1][0],
            }

2 个答案:

答案 0 :(得分:0)

您应该将选择字段设置为功能字段并调用已定义的功能,以便根据功能条件设置该字段的值。

Ex.
columns = {
    'etat_dos': fields.function(set_etat_dossier, string='Method',type='selection', selection=ETAT_DOSSIER)
    }

    def set_etat_dossier(self, cr, uid, ids, context=None):
        val = {}
        for prod in self.browse(cr, uid, ids, context=context):
            if prod.casier_judiciare ==True:
                val['etat_dos'] = ETAT_DOSSIER[0][0]
            else:
                val['etat_dos'] = ETAT_DOSSIER[1][0]

答案 1 :(得分:0)

根据您的问题,我为您的问题撰写了可能的解决方案。

def _set_etat_dossier(self, cr, uid, ids, context=None):
    retVal = {}
    for prod in self.browse(cr, uid, ids, context=context):
        if prod.casier_judiciare == True:
            retVal[ prod.id ] = ('dos_comp','Dossier Complet')
        else:
            retVal[ prod.id ] = ('dos_manq','Dossier Complet'),

    return retVal



columns = {
    'etat_dos': fields.function(_set_etat_dossier,
                                string='Etat Dossier',
                                type='selection',
                                method=True,
                                selection=ETAT_DOSSIER)
}