Odoo 中文社区

    • 注册
    • 登录
    • 搜索
    • 版块
    • 标签
    • 热门
    • 用户
    • 群组

    Odoo中文社区可以通过以下两个域名访问:shine-it.net , odoo.net.cn

    由于系统升迁的原因,本论坛部分较早期的内容存在格式和链接损坏失效的问题,并非本论坛系统本身的缺陷,望谅解

    本社区没有维护任何QQ群讨论组,任何与本社区同名的QQ群讨论组的言论与本社区无关!

    开发人员可以登录gitter讨论组: http://gitter.im/odoo-china/Talk, 需要github账号

    如果您登录系统碰到问题,请在微信公众号留言:

    One2many 还是丢了 context ?

    Odoo 开发与实施交流
    3
    8
    5254
    正在加载更多帖子
    • 从旧到新
    • 从新到旧
    • 最多赞同
    回复
    • 在新帖中回复
    登录后回复
    此主题已被删除。只有拥有主题管理权限的用户可以查看。
    • W
      wangbuke 最后由 编辑

      重庆-mrshelly(49812643) 16:35:47
      广州-步科(17779104)  15:20:00
      重庆-mrshelly(49812643) 13:09:29
      但是 one2many 还是丢了 context 的.

      @重庆-mrshelly ,总监能否发个贴 ~ ,给个具体案例,这样木有上下文的。。。

      臣妾看不懂啊 ~



      最简单的需求例子:

      在 盘点单明细上 related 产品的 qty_available 并 stored=False
      目的是, 在录入盘点单的时候, 参得到该产品在 盘点时刻 对应的库位上 的在手数量


      然后 因为要得到该库位在该时刻的该产品 的在手数量, 就需要将 盘点单 的日期, 与 明细表的 库位 这两个信息用 context 传递下去.


      所以, 就在 盘点单明细(stock.inventory.line) 的 qty_available 字段上添加 context

      <field name="qty_available" context="{'location': location_id, 'to_date': parent.date}" />


      然后 在 产品(product.product) 的 qty_available function 字段上断点, 看 context 的值.

      1 条回复 最后回复 回复 引用 0
      • W
        wangbuke 最后由 编辑

        根据总监的意思,写了个小模块,详情请下载附件。部分代码如下:

        stock.py

        <br /><br />from openerp.osv import osv, fields<br /><br />class stock_inventory_line(osv.osv):<br />&nbsp; &nbsp; _inherit = &quot;stock.inventory.line&quot;<br />&nbsp; &nbsp; _columns = {<br />&nbsp; &nbsp; &nbsp; &nbsp; &#039;qty_available&#039;: fields.related(&#039;product_id&#039;,&#039;qty_available&#039;,type=&#039;float&#039;,string=&#039;Qty Available&#039;),<br />&nbsp; &nbsp; }<br /><br />
        



        stock_view.xml

        <br /><br />&nbsp; &nbsp; &nbsp; &nbsp; &lt;record model=&quot;ir.ui.view&quot; id=&quot;view_inventory_qty_form&quot;&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;field name=&quot;name&quot;&gt;stock.inventory.form&lt;/field&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;field name=&quot;model&quot;&gt;stock.inventory&lt;/field&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;field name=&quot;inherit_id&quot; ref=&quot;stock.view_inventory_form&quot; /&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;field name=&quot;arch&quot; type=&quot;xml&quot;&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;field name=&quot;product_qty&quot; position=&quot;after&quot;&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;field name=&quot;qty_available&quot; context=&quot;{&#039;location&#039;: location_id, &#039;to_date&#039;: parent.date}&quot; /&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/field&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/field&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &lt;/record&gt;<br /><br />
        
        1 条回复 最后回复 回复 引用 0
        • W
          wangbuke 最后由 编辑

          首先说明下:上一贴和附件的代码,都是没能实现需求的。。。。 😎

          1、需求
          [quote]目的是, 在录入盘点单的时候, 参得到该产品在 盘点时刻 对应的库位上 的在手数量[/quote]

          理所当然是采用onchange来运算嘛, 修改 product_id view 如下:

          <field context="{'location':location_id, 'uom':product_uom, 'to_date':parent.date}" name="product_id" on_change="on_change_product_id(location_id,product_id,product_uom,parent.date, context)"  domain="[('type','&lt;&gt;','service')]"/>

          注:on_change_product_id 加了 context 参数,因此 on_change_product_id 也必须改改:
              def on_change_product_id(self, cr, uid, ids, location_id, product, uom=False, to_date=False, context=None):
                  """ Changes UoM and name if product_id changes.
                  @param location_id: Location id
                  @param product: Changed product_id
                  @param uom: UoM product
                  @return:  Dictionary of changed values
                  """
                  context = {} if context is None else context
                  if not product:
                      return {'value': {'product_qty': 0.0, 'product_uom': False, 'prod_lot_id': False}}
                  obj_product = self.pool.get('product.product').browse(cr, uid, product, context=context)
                  uom = uom or obj_product.uom_id.id
                  amount = self.pool.get('stock.location')._product_get(cr, uid, location_id, [product], {'uom': uom, 'to_date': to_date, 'compute_child': False})[product]
                  result = {'product_qty': amount, 'product_uom': uom, 'prod_lot_id': False,'qty_available':obj_product.qty_available}
                  return {'value': result}

          注:原函数self.pool.get('product.product').browse 没有传递 context , 这就应该是所谓的丢失 context 吧

          2、带来的问题
          stock.inventory.line.qty_available 字段定义默认 store=False 的,那么如果是read stock.inventory 对象,这时没有触发 onchange 事件。那怎么办?建议是修改字段定义 store = True

          'qty_available': fields.related('product_id','qty_available',type='float',string='Qty Available', store=True),


          3、为毛 <field name="qty_available" context="{'location': location_id, 'to_date': parent.date}" /> 定义的 context 木有用?

          其实context 的用法并不多
          1、widget  有 model dataset 的,
          a) 默认值用法。可设置默认值default_xxx/搜索默认值search_default_xxx
          b) 该 model 的 orm 用法,如 name_search 方法等

          2、 on_change_xxx 用法
          <field name="xxx" context="{'xxx': xxx}"  on_change="on_change_xxx(a,bc,c, context)/>


          回到stock.inventory.line.qty_available 字段,该字段在JS端会被解析为 instance.web.form.FieldFloat widget , 无model 无dataset ,你说context 给它有啥用呢。。。。



          欢迎讨论 ~

          1 条回复 最后回复 回复 引用 0
          • W
            wangbuke 最后由 编辑

            再次回到需求,,,

            [quote]目的是, 在录入盘点单的时候, 参得到该产品在 盘点时刻 对应的库位上 的在手数量[/quote]

            其实我觉得更好的实现是用 qty_available 使用 function fileld 来实现, 对于 stock.inventory.line 来说,location 和 to_date 都是已知的。

            这样的话,每一行都会单独计算,就不必纠结 store=True/False 的问题了。

            1 条回复 最后回复 回复 引用 0
            • C
              ccdos 最后由 编辑

              不明觉历,留名 备查

              1 条回复 最后回复 回复 引用 0
              • mrshelly
                mrshelly 最后由 编辑

                保存单据, 再重新打开.. .看看字段数据...

                1 条回复 最后回复 回复 引用 0
                • mrshelly
                  mrshelly 最后由 编辑

                  on_change 事件里可以得到 context..

                  打开单据后, 单据的 one2many 字段, 并没有得到传入的 context 导致 数量未按预想的计算.

                  1 条回复 最后回复 回复 引用 0
                  • First post
                    Last post