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树和表单视图如下
<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>
我要做的是获取price_perUoM模型中具有相同配方名称的每个行条目的所有总和,或者recipe_id在另一个拆分视图中查看它们。
这样我就可以有一个显示所有字段的树视图,另一个只显示该特定配方的配方名称和总成本
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。