Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
2 Trả lời
3754 Lượt xem

hello,

I have some data and I need a new field (maybe not?).

this field  gets calculated by the count of items divided by the sum of a field from the counted items.

this should be displayed in a pivot view.


So my question is, is there a nice way without adding a computed field with store = true and get this shown in the pivot view (so column c = column a / column b; using group by)?


if not, how is it about with store = true > because of the group by it's always different 


best
Max

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

Hello Max,

I have also added one compute field in pivot view of sale order.

class SaleOrder(models.Model):
_inherit = 'sale.order'

@api.depends('partner_id')
def _compute_method_name(self):
.....
field_name = fields.Char(string="Field Name",compute="_compute_method_name", store=True)

So after that add that field in sale order form view

For add the compute field in pivot view of sale order, and same field in sale.report and  inherit query of sale report

class SaleReport(models.Model):
_inherit = 'sale.report'
 field_name = fields.Char(string="Field Name")

def _query(self, with_clause='', fields=None, groupby='', from_clause=''):
if fields is None:
fields = {}
fields['field_name'] = ", s.field_name as field_name"
return super(SaleReport, self)._query(with_clause, fields, groupby, from_clause)

This works fine for me, I hope this will works for you too.........


Ảnh đại diện
Huỷ bỏ

Is it possible to add a compute many2many field to the pivot report?
@Meet

Câu trả lời hay nhất

Hello,@Meet's answer is very good, but it doesn't solve all the cases. For instance Pivot views struggle with calculating an average value from the percentage fields. E.g. we have a percentage margin field on each sale.order.line.

If we add this field to the SaleReport just as @Meet shows, we are getting a completely messed up grouped results. One line with -50000% margin (with $0.2 value!) will ruin thousands of other % margin line values.

The only solution I see would be to add the calculated field into the SaleReport model instead of grouping the percentage values of the source lines. But so far I couldn't make it to work. It should be rather simple, but apparently it's not. We're getting the column not found error, while we are not fetching it from SQL at all..

Ảnh đại diện
Huỷ bỏ