Odoo模型继承了模型更改默认值

时间:2016-02-16 22:33:21

标签: model openerp odoo-8

我正在尝试更改继承模型字段的默认值。 我试图更改的模型是account.invoice,如下所示:

class account_invoice(models.Model):
    _inherit = 'account.invoice'

    defaults = {
        'journal_id': None,
    }

原因是客户有几种期刊,并担心某些员工忘记更换正确的期刊。我需要手动选择它

编辑: 我正在使用此代码,但_default_currency不会覆盖

from openerp import fields, models, osv, api, _
from openerp.osv import osv,fields as Fields


TYPE2JOURNAL = {
    'out_invoice': 'sale',
    'in_invoice': 'purchase',
    'out_refund': 'sale_refund',
    'in_refund': 'purchase_refund',
}

class account_invoice(models.Model):
    _inherit = 'account.invoice'

    @api.model
    def _default_journal(self):        
        return None 

    @api.model
    def _default_currency(self):
        inv_type = self._context.get('type', 'out_invoice')
        inv_types = inv_type if isinstance(inv_type, list) else [inv_type]
        company_id = self._context.get('company_id', self.env.user.company_id.id)
        domain = [
            ('type', 'in', filter(None, map(TYPE2JOURNAL.get, inv_types))),
            ('company_id', '=', company_id),
        ]        
        journal = self.env['account.journal'].search(domain, limit=1) 
        return journal.currency or journal.company_id.currency_id

1 个答案:

答案 0 :(得分:0)

原始py上的默认值定义为名为_default_journal的函数

@api.model
def _default_journal(self):
    inv_type = self._context.get('type', 'out_invoice')
    inv_types = inv_type if isinstance(inv_type, list) else [inv_type]
    company_id = self._context.get('company_id', self.env.user.company_id.id)
    domain = [
        ('type', 'in', filter(None, map(TYPE2JOURNAL.get, inv_types))),
        ('company_id', '=', company_id),
    ]
    return self.env['account.journal'].search(domain, limit=1)

重新定义它并在回报中加上“无”。

否则,您可以重新定义该字段:

journal_id = fields.Many2one('account.journal', string='Journal',
    required=True, readonly=True, states={'draft': [('readonly', False)]},
    default=_default_journal,
    domain="[('type', 'in', {'out_invoice': ['sale'], 'out_refund': ['sale_refund'], 'in_refund': ['purchase_refund'], 'in_invoice': ['purchase']}.get(type, [])), ('company_id', '=', company_id)]")

只需复制相同内容并删除默认行。

如果您执行第一个解决方案,请务必小心,因为默认货币来自默认日记帐:

@api.model
def _default_currency(self):
    journal = self._default_journal()
    return journal.currency or journal.company_id.currency_id

请务必重新定义此功能以及解析默认日记代码以获取所需的货币。