Přejít na obsah
Menu
You need to be registered to interact with the community.
This question has been flagged
1 Odpovědět
2551 Zobrazení

Hello! 

How to add percentage column of two other columns in pivot view?

When I tried to add from the SQL query, only 1 row was resolved correctly (the one below), for the rest of the rows it was added with the value of the previous row. 

Thanks in advance.

Avatar
Zrušit
Nejlepší odpověď

Hi,
Refer the code
from odoo import models, fields, api

class YourModel(models.Model):
    _name = 'your.model'
    _description = 'Your Model'

    field1 = fields.Float(string='Field 1')
    field2 = fields.Float(string='Field 2')
    percentage = fields.Float(string='Percentage', compute='_compute_percentage', store=True)

    @api.depends('field1', 'field2')
    def _compute_percentage(self):
        for record in self:
            if record.field2 != 0:
                record.percentage = (record.field1 / record.field2) * 100
            else:
                record.percentage = 0.0

<record id="view_your_model_pivot" model="ir.ui.view">
<field name="name">your.model.pivot</field>
<field name="model">your.model</field>
    <field name="arch" type="xml">
        <pivot>
            <field name="field1" type="measure" string="Field 1"/>
            <field name="field2" type="measure" string="Field 2"/>
            <field name="percentage" type="measure" string="Percentage"/>
        </pivot>
    </field>
</record>


Hope it helps

Avatar
Zrušit
Autor

Thank you Cybrosys Techno Solutions Pvt.Ltd, but the complexity lies in the fact that to solve this pivot I have had to create a view, something non-persistent, because I cannot rely on a single model and it turns out that I need to obtain a percentage from two fields of this query.

When I put the formula in my query, everything is fine, but when it is displayed in the pivot it adds the percentages of the grouped rows.

I mean that if one of the groups has a row and the percentage is 100, the result in the pivot is 100. If in the group there are two records whose percentages have a value of 100, the result shown is 200, which is incorrect.

Related Posts Odpovědi Zobrazení Aktivita
1
čvn 25
1605
3
čvc 25
3254
1
kvě 25
1393
1
kvě 25
1643
4
kvě 25
2810