英文:
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 = ['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)
And here is my XML File:
<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>
> 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 = ['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',)
<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"/>
<field name="state_tax" attrs="{'invisible':[('state_tax','==', 0.00)]}" readonly="True"/>
<field name="county_tax" attrs="{'invisible':[('county_tax','==', 0.00)]}" readonly="True"/>
<field name="city_tax" attrs="{'invisible':[('city_tax','==', 0.00)]}" readonly="True"/>
<field name="unincorporated_tax" attrs="{'invisible':[('unincorporated_tax','==', 0.00)]}" readonly="True"/>
</xpath>
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.
<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 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="{'invisible': [('federal_tax','=', 0.00)]}"
注意:
我没有看到任何税费字段的计算,并且您已将其声明为具有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="{'invisible': [('federal_tax','=', 0.00)]}"
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论