Set default value of field depends on other field

2019-09-13 10:27发布

I am setting up default value of analytics_id in account.move.line by below code

class account_move_line(models.Model):
    _inherit = 'account.move.line'
    _name = "account.move.line"

    def _get_default_account(self, cr, uid, context=None):
        obj = self.pool.get('account.move')
        value = obj.browse(cr, uid, uid)
        if  value.move_id.debit>0 or value.move_id.credit<0:
            res = self.pool.get('account.analytic.plan.instance').search(cr, uid, [('code','=','LAL')], context=context)
            return res and res[0] or False

    _defaults = {
        'analytics_id': _get_default_account,
    }

it is working well for me but now i want to set this default value if debit field value is greater then zero OR credit field value less then zero otherwise analytics_id field remain empty.

1条回答
神经病院院长
2楼-- · 2019-09-13 10:50

Try to use this type of code

res = self.pool.get('account.analytic.plan.instance').search(cr, uid, [('code','=','LAL')], context=context)
if res:    
   br_analytic_plan=self.pool.get('account.analytic.plan.instance').browse(cr,uid,res[0],context=context)
   if ---your Condition---- # You can access For example. br_analytic_plan.amount > 0
       return res[0]

This same logic you can apply to both groups condition (it means under if and else on your current code).

Hope this helps.

查看更多
登录 后发表回答