Set default value of field depends on other field in odoo

馋奶兔 提交于 2020-01-17 06:32:08

问题


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:


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.



来源:https://stackoverflow.com/questions/38524745/set-default-value-of-field-depends-on-other-field-in-odoo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!