zoukankan      html  css  js  c++  java
  • (61) 总结字段更新方法

    前言:

    当我们建立一个记录后,随着后面的流程,这个记录有些字段是要更改的

     

    采用onchange更改

    @api.onchange('sale_id')
    def onchange_sale_id(self
    ):
       
    if self
    .sale_id:
           
    self.sale_order_site = self
    .sale_id.sale_order_site

    sale_id = fields.Many2one(
    'sale.order', 'Sale Order'
    ,
                             
    domain=lambda self: [('user_id', '=', self.env.uid),('state','!=','cancel'
    )],
                              )

     

    clip_image001

    当我们改变销售订单,对应的网单号就会跟着变

     

     

    采用compute 更改

    @api.one
    @api.depends('amount','back_amount','currency_id','payment_date'
    )
    def _get_amount_net(self
    ):
        amount_net =
    0.0
       
    currency = self.env['res.currency'].search([('name', '=', 'USD')], limit=1
    )
       
    if
    currency:
            amount_net_orig =
    self.amount - self
    .back_amount
           
    if
    amount_net_orig:
               
    if currency.id != self
    .currency_id.id:
                    from_currency =
    self.currency_id.with_context(date=self
    .payment_date)
                    amount_net += from_currency.compute(amount_net_orig, currency)
               
    else
    :
                    amount_net = amount_net_orig
       
    self
    .amount_net = amount_net

     

    amount_net = fields.Float(compute='_get_amount_net', string='Amount Net',digits_compute=dp.get_precision('Account'),store=True)

     

    这样就会根据 'amount','back_amount','currency_id','payment_date' 的变化来计算

    amount_net 的值,这里要注意,要改变的字段,一定要调用这个计算方法 如:

    compute='_get_amount_net', 若不加在指定的字段上,你在方法中写self.amount_net = xxx  这样是不会保存在数据库中的

     

    采用其它操作时更改

     

    比如当我取消订单时 要改变记录一些字段的值

    if order.apply_type =='cancel':
        yj_robot_id =
    self.env.ref('base.user_yj_robot'
    ).id
        order.sudo(yj_robot_id).action_cancel()
       
    if
    order.agreed_apply_users_type:
            order.sudo(yj_robot_id).agreed_apply_users_type =
    None
        if
    order.payment_state:
            order.sudo(yj_robot_id).payment_state =
    'cancel'

    clip_image003

     

     

    注意事项:

    权限很麻烦 ,有时仓库在出货,要改变销售订单的出货状态,这要求仓库人员有 写的权限

    但我们又想,给他们,这时就在代码中临时给一个高权限的用户操作  采用 sudo()方法来操作

    比如我上面

    order.sudo(yj_robot_id).payment_state = 'cancel'

    yj_robot_id  这是我自己建立的一个高权限用户,专门用来处理少权限的操作

  • 相关阅读:
    中国首届React开发者大会 8月18日 广州举行
    事件循环:Flutter 中代码是如何执行和运行的
    大前端趋势所向:这么多跨端技术,为什么选择 Flutter?
    通往大前端的一把关键钥匙 Flutter
    如何选一部好的手机?性价比高的智能手机推荐,2020智能手机排行榜!
    智能手机边充电边玩对电池有什么损害吗?
    你的智能手机究竟能用多久?
    新型添加技术
    智能手机
    姐姐不愧是姐姐,快看《乘风破浪的姐姐》
  • 原文地址:https://www.cnblogs.com/toby2chen/p/8358967.html
Copyright © 2011-2022 走看看