模型 odoo11 中列条目的总和
Sum of column entries in a model odoo11
我有一个模型如下,
class RecipeInformation(models.Model):
_name = 'model.recipe.information'
_description = 'recipe and ingredient information'
recipe_id = fields.Many2one('model.recipe', string='Recipe', required=True)
ingredient_id = fields.Many2one('model.ingredient', string='Ingredient', required=True)
quantity = fields.Integer('Quantity', required=True, default=1)
price = fields.Float('Price', required=True)
unit_of_measure = fields.Selection([('milligrams', 'Milligrams'), ('grams', 'Grams'),('kilograms', 'Kilograms')],
'Unit of Measure', default='grams',
required=True)
price_per_UoM = fields.Float('Price/UoM', compute='_calculate_price', store=True)
Cost = fields.Float('Total Cost')
@api.depends('price', 'quantity')
def _calculate_price(self):
for record in self:
record.price_per_UoM = record.price / record.quantity
return record.price_per_UoM
@api.depends('ingredient_quantity', 'price_per_UoM')
def _calculate_cost(self):
for record in self:
record.Cost += record.ingredient * record.price_per_UoM
return record.Cost
和 xml 树和表单视图如下
<record id="view_form_recipe_cost" model="ir.ui.view">
<field name="name">recipe.cost.form.view</field>
<field name="model">model.recipe.information</field>
<field name="arch" type="xml">
<form string="Recipe Cost form view">
<sheet>
<group>
<field name="recipe_id"/>
<field name="ingredient_id"/>
<field name="quantity"/>
<field name="price"/>
<field name="unit_of_measure"/>
<field name="price_per_UoM"/>
</group>
</sheet>
</form>
</field>
</record>
<record id="view_tree_recipe_cost" model="ir.ui.view">
<field name="name">recipe.cost.tree.view</field>
<field name="model">model.recipe.information</field>
<field name="arch" type="xml">
<tree>
<field name="recipe_id"/>
<field name="ingredient_id"/>
<field name="quantity"/>
<field name="price"/>
<field name="unit_of_measure"/>
<field name="price_per_UoM"/>
</tree>
</field>
</record>
我想要做的是获取模型中具有相同配方名称或 recipe_id
的每个行条目的所有 price_perUoM
的总和,并可能在另一个拆分视图中查看它们.
这样我就可以有一个显示所有字段的树视图
另一个只显示该特定食谱的食谱名称和总成本
我是 odoo 的新手,非常感谢任何(建议、评论)帮助。
提前致谢
有一个属性名称 sum="Total",您可以将其应用于 xml 树视图,它显示特定列
显示的所有记录的总和
我有一个模型如下,
class RecipeInformation(models.Model):
_name = 'model.recipe.information'
_description = 'recipe and ingredient information'
recipe_id = fields.Many2one('model.recipe', string='Recipe', required=True)
ingredient_id = fields.Many2one('model.ingredient', string='Ingredient', required=True)
quantity = fields.Integer('Quantity', required=True, default=1)
price = fields.Float('Price', required=True)
unit_of_measure = fields.Selection([('milligrams', 'Milligrams'), ('grams', 'Grams'),('kilograms', 'Kilograms')],
'Unit of Measure', default='grams',
required=True)
price_per_UoM = fields.Float('Price/UoM', compute='_calculate_price', store=True)
Cost = fields.Float('Total Cost')
@api.depends('price', 'quantity')
def _calculate_price(self):
for record in self:
record.price_per_UoM = record.price / record.quantity
return record.price_per_UoM
@api.depends('ingredient_quantity', 'price_per_UoM')
def _calculate_cost(self):
for record in self:
record.Cost += record.ingredient * record.price_per_UoM
return record.Cost
和 xml 树和表单视图如下
<record id="view_form_recipe_cost" model="ir.ui.view">
<field name="name">recipe.cost.form.view</field>
<field name="model">model.recipe.information</field>
<field name="arch" type="xml">
<form string="Recipe Cost form view">
<sheet>
<group>
<field name="recipe_id"/>
<field name="ingredient_id"/>
<field name="quantity"/>
<field name="price"/>
<field name="unit_of_measure"/>
<field name="price_per_UoM"/>
</group>
</sheet>
</form>
</field>
</record>
<record id="view_tree_recipe_cost" model="ir.ui.view">
<field name="name">recipe.cost.tree.view</field>
<field name="model">model.recipe.information</field>
<field name="arch" type="xml">
<tree>
<field name="recipe_id"/>
<field name="ingredient_id"/>
<field name="quantity"/>
<field name="price"/>
<field name="unit_of_measure"/>
<field name="price_per_UoM"/>
</tree>
</field>
</record>
我想要做的是获取模型中具有相同配方名称或 recipe_id
的每个行条目的所有 price_perUoM
的总和,并可能在另一个拆分视图中查看它们.
这样我就可以有一个显示所有字段的树视图 另一个只显示该特定食谱的食谱名称和总成本
我是 odoo 的新手,非常感谢任何(建议、评论)帮助。
提前致谢
有一个属性名称 sum="Total",您可以将其应用于 xml 树视图,它显示特定列
显示的所有记录的总和