Skip to Content
Menu
This question has been flagged
3 Replies
9006 Views

There is a way in odoo v8 to show the percentages in the graph view, for example, if I filter a model by sex, that I can see 80% women and 20% men, and if I apply another filter such as age> 43, keep the result in porcent, currently only graphics me the sum of the values filtered.

Avatar
Discard
Best Answer

The thing is that the results to render graph view is tied to the use of read_group method using the group_operator argument of the field definition that by default is sum when you don't define the group_operator. That field argument only works with aggregated functions in postgresql and percent is not an aggregated function for postgresql.

You could do it by overriding the read_group method of the graph view model to manually change the field value for the result. Like:

def read_group(self, cr, uid, domain, fields, groupby, offset=0, limit=None, context=None, orderby=False, lazy=True):

read_group_res = super(your_model self).read_group(cr, uid, domain, fields, groupby, offset=offset, limit=limit, context=context, orderby=orderby, lazy=lazy)

for res in read_group_res:

domain_filter = res.get('__domain', [])

line_ids = self.search(cr, uid, domain_filter, context=context)

if line_ids:

res_id = self.browse(cr, uid, line_ids[0], context=context)

res['results'] = res_id.results

return read_group_re

As the docstring of the read_group methods states:

:return: list of dictionaries(one dictionary for each record) containing:

* the values of fields grouped by the fields in ``groupby`` argument

* __domain: list of tuples specifying the search criteria

* __context: dictionary with argument like ``groupby``

Use this example to build your own solution based on your needs



Avatar
Discard

Why don't you write about the 'graph view model'? I don't find a model who has 'graph' in the name...

it's not a model, it's a js view classes that implements the graph views behaviors

My override of read_group method works now in the stock.quant model. I can see log of the variable and the returned dictionnary.

To display percentage instead/with the qty, do I have to create a new field in the model?

Should we instead modify the 'qty' in the dictionnary?

Do you know other urls that could show a complete example of displaying percentage on pie graph?

Thanks

You could see this complete example https://github.com/odoo/odoo/blob/9.0/addons/product_margin/product_margin.py#L12-L115

You could modify the qty value but better create another field because it's a field that it's used for a lot of calculations and perhaps you could be altering results in some ways as read_group is not just used for graph views, just create another field

Author Best Answer

Ibra thanks very much for your answer but the problem is that i need see the porcentage related whit all my models in a graph view, for example if i have 5 employees, 3 male and 2 female when i filter for sex in my graph view for hr_employee model i wan to see the result of all my models in porcent, not the quantity like by default odoo work. 

Avatar
Discard
Best Answer

Hello Oscar,

you can use a widget called progressbar for this :

<field name="your_field" widget="progressbar"/>

to show the percentages, your field should be computed.
example :  add the percentage of taken seats

you .py :

seat = fields.Integer(string="Number of seats")

attendees_ids = fields.Many2many('res.partner', string="Attendees")

taken_seats = fields.Float(string="Taken seats", compute='_taken_seats')

then you add the decorator depends because taken seats depends on both seats and attendees

@api.depends('seat', 'attendees_ids')

def _taken_seats(self):

    for r in self:

        if not r.seat:

            r.taken_seats = 0.0

   else:

        r.taken_seats = 100.0*len(r.attendees_ids)/r.seat

your .xml :

<field name= "taken_seats" widget="progressbar"/>

this is a simple example on how to dislay percentage. Try to work on your own example with the same logic and tell us if it works.

Best regards. 

Avatar
Discard
Related Posts Replies Views Activity
1
Mar 18
2805
0
Jul 17
2251
0
Mar 15
4313
0
May 24
875
0
Jul 16
3463