3

I need to remove the sum header of some columns that it automatically calculated for me.

I created a new tree view without inheriting any view, and have the fields like so:

. . .
<field name="model">my.purchase.order.line.inherit</field>
. . .
  . . .
  <field name="product_uom_qty"/>
  <field name="price_unit"/>
  . . .
  <field name="price_subtotal" widget="monetary"/>
  . . .
. . .

Column Sum

The main model is from purchase.order.line.

Now with the answer from CZoellner, my model is now something like this.

class purchase_order_line_inherit(models.Model):
    _name = "my.purchase.order.line.inherit"
    _inherit = "purchase.order.line"

    product_uom_qty = fields.Float(group_operator="min")

I think the system just calculated them for me but the point is that I want to change it from sum to min.

I have seen this but my fields (as shown earlier) do not have the sum attribute. I also have tried something like this but the sum headers are still there.

How can I achieve such task?

holydragon
  • 6,158
  • 6
  • 39
  • 62

2 Answers2

0

You can define that on field definition. In your case it would be:

my_field = fields.Float(compute="_compute_my_field", group_operator="min")

The possible operators can be found in the documentation.

group_operator (str) – aggregate function used by read_group() when grouping on this field.

Supported aggregate functions are:

array_agg : values, including nulls, concatenated into an array

count : number of rows

count_distinct : number of distinct rows

bool_and : true if all values are true, otherwise false

bool_or : true if at least one value is true, otherwise false

max : maximum value of all values

min : minimum value of all values

avg : the average (arithmetic mean) of all values

sum : sum of all values

CZoellner
  • 13,553
  • 3
  • 25
  • 38
  • I tried this method, but it does not work. The sum header is still there and sum the values instead of using min. It looks like nothing happened to the field. I also have to add that the model I am using is from "purchase.order.line", not my own. – holydragon Mar 03 '21 at 10:05
  • Could you add your "new" code to your question? It's difficult to understand what exactly was done by you. – CZoellner Mar 03 '21 at 10:41
  • Just remove the `_name` line, because you're creating a new model instead of extenting/changing one by having different `_inherit` and `_name` values. – CZoellner Mar 03 '21 at 10:48
  • But I do not want to extend the purchase.order.line model because there are multiple views that are using the model with the correct sum header as it is. Therefore, I want to change just the sum header in this view only, without anything affecting other views that are using this particular model. That is why I intended to create a new one. – holydragon Mar 03 '21 at 10:50
  • 1
    Ah okay, then my solution isn't the one you need. – CZoellner Mar 03 '21 at 10:55
  • Then you probably have to override `read_group()` and switch the operator depending of a context flag or something. Not that easy i guess. – CZoellner Mar 03 '21 at 10:57
  • I have my custom action window ready for any changes from that component, but I don't know how to do the `read_group()` thing. – holydragon Mar 03 '21 at 10:59
  • Sorry that would be too much to write down. Overriding that method isn't so easy. – CZoellner Mar 03 '21 at 12:20
0

This is the solution that solves this problem for me as recommended by many people.

You need to inherit read_group method and give it the value you want to display in the header column for the fields you want to customize.

This is the starter template of where to begin. Reference

class your_class(models.Model):
    # ...        
    @api.model 
    def read_group(self, domain, fields, groupby, offset=0, limit=None, orderby=False, lazy=True):
        res = super(your_class, self).read_group(domain, fields, groupby, offset=offset, limit=limit, orderby=orderby, lazy=lazy)
        if 'invoice_due_local_currency' in fields:
            for line in res:
                if '__domain' in line:
                    lines = self.search(line['__domain'])
                    total_invoice_due = 0.0
                    for record in lines:
                        total_invoice_due += record.invoice_due_local_currency
                    line['invoice_due_local_currency'] = total_invoice_due

        return res
holydragon
  • 6,158
  • 6
  • 39
  • 62