Skip to Content
Menu
Musisz się zarejestrować, aby móc wchodzić w interakcje z tą społecznością.
To pytanie dostało ostrzeżenie
1 Odpowiedz
2564 Widoki


I have calculated 2 fields with my method. it calculates the number of products that are assigned.

class ProductProduct(models.Model):
    _inherit = 'product.product'

    planned_qty_cust = fields.Float(compute='_calculate_planned_qty', string='Planned Qty Customer', )
    planned_qty_supl = fields.Float(compute='_calculate_planned_qty', string='Planned Qty Suplier', )


    @api.multi
    def _calculate_planned_qty(self):
        for product in self:
            stock_move_obj = self.env['stock.move']
            domain = [('product_id', '=', product.id),
                                                 ('state', 'not in', ['cancel', 'done','draft']),
                                                 ('location_dest_id.usage', '=', 'customer'),
                                                 ]
            stock_moves_cust = stock_move_obj.search(domain)
            qty = sum(stock_moves_cust.mapped('product_uom_qty'))
            product.planned_qty_cust = qty
            domain2 = [('product_id', '=', product.id),
                      ('state', 'not in', ['cancel', 'done', 'draft']),
                      ('location_dest_id.usage', '=', 'supplier'),
                      ]
            stock_moves_cust = stock_move_obj.search(domain2)
            qty = sum(stock_moves_cust.mapped('product_uom_qty'))
            product.planned_qty_supl = qty

the thing is that I need to filter on these fields, and probably I need to create search function but it's kinda complicated.

maybe someone can help me with this how this method should look, or maybe there is already a module for this type of calculation?


Awatar
Odrzuć
Najlepsza odpowiedź

Hi.

Are you asking how to include this newly added fields to the search view of the corresponding model(product.product here)? Compute fields cannot be added in the search view, for that you have to give store=True attribute for the fields.


planned_qty_cust = fields.Float(compute='_calculate_planned_qty', string='Planned Qty Customer', store=True)
planned_qty_supl = fields.Float(compute='_calculate_planned_qty', string='Planned Qty Supplier', store=True)


Once you finished the above you can inherit the search view defined for the product.product model and add these new fields to it. For that you can refer this blog : Inherit Search View


Thanks 

Awatar
Odrzuć
Autor

The problem with your approach is that with stote=True this fields will be recalculated 1 time. I need to add @api.depends. but in my case i need them to be recalculated every time when view is updated. So i have only 1 option left. It is to add search=search_function to them. That what i ask. A help to create this search function

Powiązane posty Odpowiedzi Widoki Czynność
1
gru 18
2777
1
kwi 17
4902
1
lip 25
530
Search a message Rozwiązane
1
lut 25
1249
0
wrz 23
2194