Odoo 8:在安装/升级时运行的方法中浏览另一个模块

时间:2016-12-30 08:47:06

标签: odoo-8 openerp-8

我编写了一个模块,在安装/升级时需要初始数据填充。方法(_initialize_history_prices)成功启动,但current_price_records似乎没有任何值,因此它什么都不做(该表有数千条记录)。我看到它运行时没有错误。我正在做什么错误,或者在模块安装/升级期间是否无法浏览其他模块,我应该使用SQL?

以下是为简洁而截断不相关部分的代码

class pricelist_partnerinfo_history(models.Model):
    _name = 'pricelist.partnerinfo.history'

    @api.model
    def _initialize_history_prices(self):
        '''Add missing current prices in historical records.'''
        current_price_records = self.env['pricelist.partnerinfo'].browse()
        for rec in current_price_records:
            # Do stuff

pricelist_history_init.xml

<?xml version="1.0"?>

<openerp>
  <data>
    <!-- Initialize price list history records with current prices if they are missing in history -->
    <function model="pricelist.partnerinfo.history" name="_initialize_history_prices"/>
  </data>
</openerp>

__的OpenERP __。PY

'depends': ['product', 'purchase_automation'],
'data': [
    'pricelist_history_init.xml',
    'pricelist_view.xml',
],

1 个答案:

答案 0 :(得分:1)

<_>在_initialize_history_prices()方法中,在current_price_records中你会获得pricelist.partnerinfo的空记录集,因为没有id的browse()会返回空记录集,所以当这个方法调用时什么都不会发生

获取所有记录,您可以使用search()方法

@api.model
def _initialize_history_prices(self):
    '''Add missing current prices in historical records.'''
    current_price_records = self.env['pricelist.partnerinfo'].search([])
    for rec in current_price_records:
        # Do stuff