Search code examples
pythonxmlodoo-11

Sum of column entries in a model odoo11


I have a model as follows,

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

and an xml tree and form view as follows

<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>

What I am trying to do is get the sum of all the price_perUoM for each row entry in my model that has the same recipe name or recipe_id and view them possibly in another split view.

so that I can have a tree view displaying all the fields and another displaying only the recipe name and total cost of that particular recipe

I am new to odoo and I would appreciate any (advice, comments) help.

thanks in advance


Solution

  • There is an attribute name sum="Total" which you can apply on your xml tree view which show the sum of all the records showing for particular column