覆盖方法中缺少Odoo 9上下文值

时间:2017-06-26 20:47:33

标签: openerp odoo-9

在odoo9中我覆盖了search_read方法。超级方法工作正常。返回的数据我想制作一个过滤器,过滤器在上下文中,该值在点击从视图出来的按钮时被赋予。

    <button name="status_instalacion" string="Instalación" type="action" icon="fa-wrench  fa-2x"  context="{'stage_id' : 1, 'current_id': active_id}"/> 

当我在search_read方法中查询上下文时,会出现问题。它存在,但没有我放置的值

单击按钮的上下文:

  
    
      

self._context       {u&#39; lang&#39;:你&#39; en_US&#39;,你&#39; stage_id&#39;:1,你&#39; t:&#39;:错误,你&#39; uid&#39;: 1,你&#39; current_id&#39;:40,你&#39; tipo_validacion&#39;:你&#39; Sistemas Cr \ xedticos&#39;,你&#39; sistema_critico&#39;:你&#39; AGUA&#39; ;}

    
  

stage_id是我想要的值

read_search上的

上下文:

  
    
      

self._context       {u&#39; lang&#39;:你&#39; en_US&#39;,你&#39; bin_size&#39;:是的,你&#39; tipo_validacion&#39;:你&#39; Sistemas Cr \ xedticos&#39; ,你&#39;:错,你&#39;:1,       你好主意&#39;:错,你&#39; sistema_critico&#39;:你&#39; AGUA&#39;}

    
  

你可以看到&#39; stage_id&#39;价值缺失

尝试将值分配给类的属性,但值永远不会改变它始终是初始值。

from logging import getLogger
from openerp import api, fields, models

_logger = getLogger(__name__)


class MgmtsystemSistemasEquipos(models.Model):
    """ Equipos."""

    _name = 'mgmtsystem.sistemas.equipos'
    dmy = 99  # ---> this value never changes

    def dummy(self):       # ---> tried calling a function. not work
        return self.dmy

    def set_dummy(self, id):      # ----> set the value
        self.dmy = id or self.dmy

    codigo = fields.Char(
        string=u'Código',
        help=u"Código equipo",
        required=True,
        size=30)
    name = fields.Char(
        string=u'Nombre equipo',
        required=True,
        readonly=False,
        index=True,
        help="Nombre corto equipo",
        size=30)
    stage_id = fields.Many2one(
        'mgmtsystem.action.stage',
        'Fase',
        default=_default_stage,
        readonly=True)


    @api.multi
    def status_instalacion(self):

        import pudb
        pu.db

        # save value to variable dmy to retrieve later
        id = self._context.get('stage_id')
        self.set_dummy(id)



    @api.model
    def search_read(
            self, domain=None, fields=None, offset=0,
            limit=None, order=None):

        import pudb
        pu.db

        # here the variable allways has the original value (99)
        current_stage_id = self.dmy
        current_stage_id = self.dummy()
        current_stage_id = getattr(self, dmy)


        res = super(MgmtsystemSistemasEquipos, self).search_read(
            domain, fields, offset, limit, order)

        current_id = res[0]['id']

        valid_protocols_ids = self._get_ids(
            current_stage_id, current_id,
            'mgmtsystem_equipos_protocolos',
            'mgmtsystem_equipos_protocolos_rel',
            'protocolo_id')

        # # remove ids
        res[0]['protocolos_ids'] = valid_protocols_ids
        res[0]['informes_ids'] = valid_informes_ids
        res[0]['anexos_ids'] = valid_anexos_ids

        return res


    # @api.multi
    def _get_ids(self, current_stage_id, current_id, model, model_rel, field_rel):

        import pudb
        pu.db

        # in this method the value of the variable is allways the original
        current_stage_id = self.dummy()

        sql = """ select a.id from
            %s as a
            join %s as b
            on a.id = b.%s where b.equipo_id = %s
            and a.stage_id = %s; """ % (model, model_rel, field_rel,
                                        current_id, current_stage_id)

        import psycopg2

        try:
            self.env.cr.execute(sql)
        except psycopg2.ProgrammingError, ex:
            message = 'Error trying to download data from server. \n {0} \n {1}'.format(ex.pgerror, sql)
            _logger.info(message)
            return False

        rows = self.env.cr.fetchall()
        list_of_ids = []

        for row in rows:
            list_of_ids.append(row[0])

        return list_of_ids

我不太了解Python,这就是我误解如何读取变量值的原因。

但话又说回来,为什么在search_read方法中修改了上下文?。

谢谢。

1 个答案:

答案 0 :(得分:0)

你应该尝试一下。

@api.model
def search_read(self, domain=None, fields=None, offset=0, limit=None, order=None):

    import pudb
    pu.db

    # Here you need to get the value from the context.
    current_stage_id = self._context.get('stage_id', getattr(self, dmy))

    res = super(MgmtsystemSistemasEquipos, self).search_read(domain=domain, fields=fields, offset=offset, limit=limit, order=order)

    current_id = res[0]['id']

    valid_protocols_ids = self._get_ids(
        current_stage_id, current_id,
        'mgmtsystem_equipos_protocolos',
        'mgmtsystem_equipos_protocolos_rel',
        'protocolo_id')

    # # remove ids
    res[0]['protocolos_ids'] = valid_protocols_ids
    res[0]['informes_ids'] = valid_informes_ids
    res[0]['anexos_ids'] = valid_anexos_ids

    return res

在你的代码中,这些行不起作用只是因为self中没有可用的记录集(正确的行为,search_read必须有@ api.model装饰器)。

    # here the variable allways has the original value (99)
    current_stage_id = self.dmy
    current_stage_id = self.dummy()
    current_stage_id = getattr(self, dmy)

所以只需删除那些和行并应用其他逻辑来获取数据。