This question has been flagged
2 Replies
5478 Views

Hello,

I have a custom model that have a relation One2many with stock.move, here's my code :

    class CpsProductTemplate(models.Model):
        _name = 'cps.product.template'

        reference = fields.Char(string="Référence")
        reception_line_ids= fields.One2many("stock.move", 'product_template_reception_id', string="Liste des récéptions", domain= '[("to_refund", "=", False)]')
        date_reception = fields.Datetime(related='reception_line_ids.date_expected', string="Date de reception")
        total_reception_one2many = fields.Integer(compute="_compute_total_reception_one2many", string="Entrées")

        @api.depends('date_reception')
        def _compute_total_reception_one2many(self):
           for p in self:
               totalentree = 0
               for entree in self.with_context().reception_line_ids.filtered(lambda t: t.product_id.id == p.product_id.id):
                   if entree.picking_id.origin is False:
                       totalentree+=entree.product_uom_qty
               p.total_reception_dynamique =totalentree

My xml tree

<record model="ir.ui.view" id="cps_product_production_tree_view">
    <field name="name">cps.product.production.tree</field>
    <field name="model">cps.product.production</field>
    <field name="arch" type="xml">
        <tree string="Commande production">
            <field name="reference"/>
            <field name="total_reception_one2many"/>
        </tree>
    </field>
</record>
  My goal is to have the sum of the field "total_reception_one2many" according to what the user have filtered in "date_reception". Is there any way to get the filter applied by the user in the search area on the tree? Thanks in advance



Avatar
Discard
Best Answer

Hi,

If you want to use the compute field in search view, then you must store the value. By using store=True.

If you set the value store true, then you might be update the value in create or write. Every time it will not compute by default.

       total_reception_one2many = fields.Integer(compute="_compute_total_reception_one2many", string="Entrées", store=True)

But in filters you can use without store=True, please use search parameter in field definition.


       total_reception_one2many = fields.Integer(compute="_compute_total_reception_one2many", string="Entrées", search=True)

Check the reference in account module.

credit = fields.Monetary(compute='_credit_debit_get', search=_credit_search,
string='Total Receivable', help="Total amount this customer owes you.")




S

Avatar
Discard