为什么数值消失?

huangapple go评论59阅读模式
英文:

Why values are disappearing?

问题

我正在Odoo13中工作,创建销售订单时计算一些税费,成功计算了相应的税费如下所示。

但在保存销售订单或确认后,所有自定义的税费字段都消失了。

以下是我的Python文件代码:

class SaleOrder(models.Model):
    _inherit = ['sale.order']

    federal_tax = fields.Float('Federal Tax', readonly=True)
    state_tax = fields.Float('State Tax', readonly=True)
    county_tax = fields.Float('County Tax', readonly=True)
    city_tax = fields.Float('City Tax', readonly=True)
    unincorporated_tax = fields.Float('Unincorporated Tax', store=True, readonly=True)

以下是我的XML文件:

<xpath expr="/form/sheet/notebook/page/group/group/field[@name='amount_untaxed']" position="after">
    <field name="federal_tax" attrs="{'invisible':[('federal_tax','=', 0.00)]}"/>
    <field name="state_tax" attrs="{'invisible':[('state_tax','=', 0.00)]}"/>
    <field name="county_tax" attrs="{'invisible':[('county_tax','=', 0.00)]}"/>
    <field name="city_tax" attrs="{'invisible':[('city_tax','=', 0.00)]}"/>
    <field name="unincorporated_tax" attrs="{'invisible':[('unincorporated_tax','=', 0.00)]}"/>
</xpath>

注意:确认订单或保存后,我的自定义字段值变为0,这就是它们消失的原因,但为什么它们会变为零?

英文:

I am working in Odoo13 I am calculating some taxes while creating my sale order and while doing this I am successfully calculating my respective taxes as following.

为什么数值消失?

But after saving my sale order or by confirming it all my custom tax fields disappear like:

为什么数值消失?

Here is my Python File code:

class SaleOrder(models.Model):
_inherit = [&#39;sale.order&#39;]

federal_tax = fields.Float(&#39;Federal Tax&#39;, readonly=True)
state_tax = fields.Float(&#39;State Tax&#39;, readonly=True)
county_tax = fields.Float(&#39;County Tax&#39;, readonly=True)
city_tax = fields.Float(&#39;City Tax&#39;, readonly=True)
unincorporated_tax = fields.Float(&#39;Unincorporated Tax&#39;, store=True, readonly=True)

And here is my XML File:

&lt;xpath expr=&quot;/form/sheet/notebook/page/group/group/field[@name=&#39;amount_untaxed&#39;]&quot; position=&quot;after&quot;&gt;
    &lt;field name=&quot;federal_tax&quot; attrs=&quot;{&#39;invisible&#39;:[(&#39;federal_tax&#39;,&#39;==&#39;, 0.00)]}&quot;/&gt;
    &lt;field name=&quot;state_tax&quot; attrs=&quot;{&#39;invisible&#39;:[(&#39;state_tax&#39;,&#39;==&#39;, 0.00)]}&quot;/&gt;
    &lt;field name=&quot;county_tax&quot; attrs=&quot;{&#39;invisible&#39;:[(&#39;county_tax&#39;,&#39;==&#39;, 0.00)]}&quot;/&gt;
    &lt;field name=&quot;city_tax&quot; attrs=&quot;{&#39;invisible&#39;:[(&#39;city_tax&#39;,&#39;==&#39;, 0.00)]}&quot;/&gt;
    &lt;field name=&quot;unincorporated_tax&quot; attrs=&quot;{&#39;invisible&#39;:[(&#39;unincorporated_tax&#39;,&#39;==&#39;, 0.00)]}&quot;/&gt;
&lt;/xpath&gt;

> Note: After confirming my order or by saving it my custom fields
> value turn to 0 that is why they are disappearing but why they are
> turning to zero?

答案1

得分: 2

你可以在视图上添加 readonly 字段属性,而不是在模型上,例如:

class SaleOrder(models.Model):
    _inherit = ['sale.order']
    
    federal_tax = fields.Float('Federal Tax',)
    state_tax = fields.Float('State Tax',)
    county_tax = fields.Float('County Tax',)
    city_tax = fields.Float('City Tax',)
    unincorporated_tax = fields.Float('Unincorporated Tax',)

现在的主要问题是,为什么你的数据没有被存储,这是因为在模型定义中,你将字段定义为只读 (readonly),默认情况下,Odoo 不会存储对于 readonly 属性设置为 True 的字段的表单数据。我假设你是通过 onchange 函数在表单视图中计算这些只读字段的值,因此数据只会在运行时计算,但尚未保存到数据库中。但由于这些字段是只读的,Odoo 会忽略这些字段的值,因此即使在保存后,你仍然得到相同的值。你可以使用 force_save 属性来覆盖Odoo的默认行为,该属性会覆盖Odoo的默认表单行为,即使字段是只读的也会提交字段的值。

<xpath expr="/form/sheet/notebook/page/group/group/field[@name='amount_untaxed']" position="after">
    <field name="federal_tax" attrs="{'invisible':[('federal_tax','=', 0.00)]}" readonly="True" force_save="1"/>
    <field name="state_tax" attrs="{'invisible':[('state_tax','=', 0.00)]}" readonly="True" force_save="1"/>
    <field name="county_tax" attrs="{'invisible':[('county_tax','=', 0.00)]}" readonly="True" force_save="1"/>
    <field name="city_tax" attrs="{'invisible':[('city_tax','=', 0.00)]}" readonly="True" force_save="1"/>
    <field name="unincorporated_tax" attrs="{'invisible':[('unincorporated_tax','=', 0.00)]}" readonly="True" force_save="1"/>
</xpath>

Odoo 对于 invisible 字段也遵循相同的行为,因此你必须在 invisible 字段中使用 force_save 属性。

英文:

You can put readonly field attribute on view instead of model, for example:

class SaleOrder(models.Model):
_inherit = [&#39;sale.order&#39;]

    federal_tax = fields.Float(&#39;Federal Tax&#39;,) 
    state_tax = fields.Float(&#39;State Tax&#39;,) 
    county_tax = fields.Float(&#39;County Tax&#39;,) 
    city_tax = fields.Float(&#39;City Tax&#39;,) 
    unincorporated_tax = fields.Float(&#39;Unincorporated Tax&#39;,)


&lt;xpath expr=&quot;/form/sheet/notebook/page/group/group/field[@name=&#39;amount_untaxed&#39;]&quot; position=&quot;after&quot;&gt;
    &lt;field name=&quot;federal_tax&quot; attrs=&quot;{&#39;invisible&#39;:[(&#39;federal_tax&#39;,&#39;==&#39;, 0.00)]}&quot; readonly=&quot;True&quot;/&gt;
    &lt;field name=&quot;state_tax&quot; attrs=&quot;{&#39;invisible&#39;:[(&#39;state_tax&#39;,&#39;==&#39;, 0.00)]}&quot; readonly=&quot;True&quot;/&gt;
    &lt;field name=&quot;county_tax&quot; attrs=&quot;{&#39;invisible&#39;:[(&#39;county_tax&#39;,&#39;==&#39;, 0.00)]}&quot; readonly=&quot;True&quot;/&gt;
    &lt;field name=&quot;city_tax&quot; attrs=&quot;{&#39;invisible&#39;:[(&#39;city_tax&#39;,&#39;==&#39;, 0.00)]}&quot; readonly=&quot;True&quot;/&gt;
    &lt;field name=&quot;unincorporated_tax&quot; attrs=&quot;{&#39;invisible&#39;:[(&#39;unincorporated_tax&#39;,&#39;==&#39;, 0.00)]}&quot; readonly=&quot;True&quot;/&gt; 
&lt;/xpath&gt;

Now the main question, why your data is not being stored, it is because on the model definition you defined the fields as readonly, by default Odoo doesn't store form data from fields for which readonly attribute is set to True. I am assuming that you are calculating those readonly fields on the form view from onchange function, so the data is only calculated on the fly, but not yet saved on database, but as this are readonly, Odoo is ignoring those fields value and hence you are always getting ) even after pressing save. You can override the Odoo default behavior using force_save attribute, which overrides Odoo default form behavior to submit fields value even if readonly.

&lt;xpath expr=&quot;/form/sheet/notebook/page/group/group/field[@name=&#39;amount_untaxed&#39;]&quot; position=&quot;after&quot;&gt;
    &lt;field name=&quot;federal_tax&quot; attrs=&quot;{&#39;invisible&#39;:[(&#39;federal_tax&#39;,&#39;==&#39;, 0.00)]}&quot; readonly=&quot;True&quot; force_save=&quot;1&quot;/&gt;
    &lt;field name=&quot;state_tax&quot; attrs=&quot;{&#39;invisible&#39;:[(&#39;state_tax&#39;,&#39;==&#39;, 0.00)]}&quot; readonly=&quot;True&quot; force_save=&quot;1&quot;/&gt;
    &lt;field name=&quot;county_tax&quot; attrs=&quot;{&#39;invisible&#39;:[(&#39;county_tax&#39;,&#39;==&#39;, 0.00)]}&quot; readonly=&quot;True&quot; force_save=&quot;1&quot;/&gt;
    &lt;field name=&quot;city_tax&quot; attrs=&quot;{&#39;invisible&#39;:[(&#39;city_tax&#39;,&#39;==&#39;, 0.00)]}&quot; readonly=&quot;True&quot; force_save=&quot;1&quot;/&gt;
    &lt;field name=&quot;unincorporated_tax&quot; attrs=&quot;{&#39;invisible&#39;:[(&#39;unincorporated_tax&#39;,&#39;==&#39;, 0.00)]}&quot; readonly=&quot;True&quot; force_save=&quot;1&quot;/&gt; 
&lt;/xpath&gt;

Odoo follows same behavior for invisible fields also, so you have to use force_save attribute in invisible fields also.

答案2

得分: 0

删除XML字段声明中的invisible属性。

重新启动Odoo服务器,升级模块。并验证您的输出。

之后,您将知道问题出在哪里。

如果您想隐藏税费,请执行以下操作。

attrs=&quot;{&#39;invisible&#39;: [(&#39;federal_tax&#39;,&#39;=&#39;, 0.00)]}&quot;

注意:

我没有看到任何税费字段的计算,并且您已将其声明为具有readonly=True属性。这意味着它将在记录保存时刷新值。

英文:

Remove invisible attribute from xml field declaration.

Restart Odoo server, upgrade module. And verify your output.

Afterwards, you will know where is problem.

If you want to hide tax, then do following.

attrs=&quot;{&#39;invisible&#39;: [(&#39;federal_tax&#39;,&#39;=&#39;, 0.00)]}&quot;

NOTE:

I haven't seen any tax fields calculation and you have declared it with readonly=True attributes. That's means it will flush value at that time of record save.

huangapple
  • 本文由 发表于 2020年1月3日 18:31:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/59576974.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定