Openerp Odoo在sale.order.line视图中设置计量单位的默认域

Openerp Odoo在sale.order.line视图中设置计量单位的默认域,openerp,views,odoo-9,Openerp,Views,Odoo 9,我一直在尝试在sale.order.line中的product_uom字段上设置默认域。编辑现有报价单时,它不会显示与默认销售单位相同类别中的计量单位。所以,如果你以单位销售,你会希望看到单位和几十个。相反,您会看到cm、g、in等。当您创建订单时,您不会遇到此问题,因为这些值是通过onchange设置的 我以为这会很简单,但我一辈子都不明白为什么我的尝试会失败。我试过: <record id="sale_order_form_view_inherit_product_uomco

我一直在尝试在sale.order.line中的product_uom字段上设置默认域。编辑现有报价单时,它不会显示与默认销售单位相同类别中的计量单位。所以,如果你以单位销售,你会希望看到单位和几十个。相反,您会看到cm、g、in等。当您创建订单时,您不会遇到此问题,因为这些值是通过onchange设置的

我以为这会很简单,但我一辈子都不明白为什么我的尝试会失败。我试过:

     <record id="sale_order_form_view_inherit_product_uomconv" model="ir.ui.view">

        <field name="name">sale.order.form.inherit</field>
        <field name="model">sale.order</field>
        <field name="inherit_id" ref="sale.view_order_form"/>
        <field name="arch" type="xml">
            <xpath expr="//field[@name='order_line']/tree/field[@name='product_uom']" position="attributes">
                <attribute name="domain">[('category_id', '=', product_id.uom_id.category_id.id)]</attribute>
            </xpath>
            </field>
     </record>

sale.order.form.inherit
销售订单
[('category_id','=',product_id.uom_id.category_id.id)]
我也试过了

      <record id="sale_order_form_view_inherit_product_uomconv" model="ir.ui.view">

        <field name="name">sale.order.form.inherit</field>
        <field name="model">sale.order</field>
        <field name="inherit_id" ref="sale.view_order_form"/>
        <field name="arch" type="xml">
            <xpath expr="//field[@name='order_line']/tree/field[@name='product_uom']" position="replace">
                <field name="product_uom" domain="[('category_id', '=', product_id.uom_id.category_id.id)]" attrs="{'readonly': [('state', 'in', ('sale','done', 'cancel'))]}" context="{'company_id': parent.company_id}" groups="product.group_uom" options='{"no_open": True}'/>
            </xpath>
            </field>
      </record>

sale.order.form.inherit
销售订单
我得到错误“未捕获错误:AttributeError:对象没有属性‘uom_id’”。我真的不明白这怎么可能是真的。字段product_id已在视图中声明,数据库将其映射到product.product,当我去编辑报价时,该字段肯定不是空的

如果您有任何建议,我们将不胜感激。

请在此查看为
产品计量单位设置域的onchange

最好的做法是在模块中重写它

视图中这样的域很容易出错,因为当您像使用
product\u id.uom\u id.category\u id.id
那样访问嵌套对象时,您并不知道满足此链的所有值是否都存在。更糟糕的是,在这种情况下,odoo的视图渲染不够智能,无法“软”处理。

在此检查为
产品计量单位设置域的onchange

最好的做法是在模块中重写它


视图中这样的域很容易出错,因为当您像使用
product\u id.uom\u id.category\u id.id
那样访问嵌套对象时,您并不知道满足此链的所有值是否都存在。更糟糕的是,在这种情况下,odoo的视图渲染不够智能,无法“软”处理。

我仍然不明白我最初的场景失败的原因。。。但我找到了一个解决办法

class NewSaleOrderLine(models.Model):
_inherit = 'sale.order.line'
#These computed fields are for calculating the domain on a form edit
relcatid = fields.Many2one(related='product_uom.category_id',store=True)
现在在xml中

<record id="sale_order_form_view_inherit_product_uomconv" model="ir.ui.view">
   <field name="name">sale.order.form.inherit</field>
   <field name="model">sale.order</field>
   <field name="inherit_id" ref="sale.view_order_form"/>
   <field name="arch" type="xml">
       <xpath expr="//field[@name='order_line']/tree/field[@name='qty_to_invoice']" position="after">
           <field name="relcatid" invisible="1"/>
       </xpath>

       <xpath expr="//field[@name='order_line']/tree/field[@name='product_uom']" position="replace">
           <field name="product_uom" domain="[('category_id', '=', relcatid)]" attrs="{'readonly': [('state', 'in', ('sale','done', 'cancel'))]}" context="{'company_id': parent.company_id}" groups="product.group_uom" options='{"no_open": True}'/>
       </xpath>
   </field>
</record>

sale.order.form.inherit
销售订单

我仍然不确定为什么这是必要的。

我仍然不明白为什么我最初的方案失败了。。。但我找到了一个解决办法

class NewSaleOrderLine(models.Model):
_inherit = 'sale.order.line'
#These computed fields are for calculating the domain on a form edit
relcatid = fields.Many2one(related='product_uom.category_id',store=True)
现在在xml中

<record id="sale_order_form_view_inherit_product_uomconv" model="ir.ui.view">
   <field name="name">sale.order.form.inherit</field>
   <field name="model">sale.order</field>
   <field name="inherit_id" ref="sale.view_order_form"/>
   <field name="arch" type="xml">
       <xpath expr="//field[@name='order_line']/tree/field[@name='qty_to_invoice']" position="after">
           <field name="relcatid" invisible="1"/>
       </xpath>

       <xpath expr="//field[@name='order_line']/tree/field[@name='product_uom']" position="replace">
           <field name="product_uom" domain="[('category_id', '=', relcatid)]" attrs="{'readonly': [('state', 'in', ('sale','done', 'cancel'))]}" context="{'company_id': parent.company_id}" groups="product.group_uom" options='{"no_open": True}'/>
       </xpath>
   </field>
</record>

sale.order.form.inherit
销售订单

我还不确定为什么有必要这样做。

谢谢你的回复。已经有一个onchange函数可以执行此操作,但是如果返回并编辑表单,则不会更改任何内容。。。所以onchange不会设置正确的域。今天我还有一个类似的问题,但它涉及动态视图。这只是一个默认视图,我想肯定会有办法做到这一点。我想我可以继承sale.order.line的模型并创建一个相关字段。然后我可以使它在xml中不可见,并在域表达式中使用它。。但是看起来不应该是这样的。谢谢你的回复。已经有一个onchange函数可以执行此操作,但是如果返回并编辑表单,则不会更改任何内容。。。所以onchange不会设置正确的域。今天我还有一个类似的问题,但它涉及动态视图。这只是一个默认视图,我想肯定会有办法做到这一点。我想我可以继承sale.order.line的模型并创建一个相关字段。然后我可以使它在xml中不可见,并在域表达式中使用它。。但似乎不应该这样。我正在考虑这个问题,我唯一没有测试的是在选择产品之前尝试更改域。。。当我调试这个时,我确实做了一条新的生产线并选择了一种产品。由于默认的onchange函数,域被覆盖。如果我在选择产品之前更改了计量单位,我不知道它是否会被更改。在这种情况下,relcatid将返回False。我想它应该可以处理,但我今晚会测试。事实上,我想我发现了另一个bug。添加其他产品会更改所有行的域,并将新行的计量单位类别设置为所有行的域。起初我认为我可能在覆盖视图时出错,但我删除了它并升级了模块。。。而且它仍然这样做。我认为视图处理程序可能有问题。事实证明,我的代码即使在relcatid=False时也运行良好。它工作得非常好,我可以完全去掉onchange函数中的域更改,一切都正常工作。不过我肯定倾向于一个bug。我正在考虑这个问题,我唯一没有测试的是在选择产品之前尝试更改域。。。当我调试这个时,我确实做了一条新的生产线并选择了一种产品。由于默认的onchange函数,域被覆盖。如果我在选择产品之前更改了计量单位,我不知道它是否会被更改。在这种情况下,relcatid将返回False。我想它应该可以处理,但我今晚会测试。事实上,我想我发现了另一个bug。添加其他产品会更改所有行的域,并将新行的计量单位类别设置为所有行的域。起初我认为我可能在覆盖视图时出错,但我删除了它并升级了模块。。。而且它仍然这样做。我认为视图处理程序可能有问题。事实证明,我的代码即使在relcatid=False时也运行良好。它起作用了