Search code examples
xmlpython-2.7odoo-8odoo

How to deal with related and computed fields in Transient Models in Odoo 8?


I have a problem which I am not able to understand. I have added two new fields to the model stock.production.lot. These fields are computed floats, and work perfect. Their names are current_qty and expected_qty.

Then, I have created a Transient Model. In this one, I created a many2one field lot_id which points to the model stock.production.lot. And I have added two float fields, also named current_qty and expected_qty, which are related to the respective ones I mentioned above.

The problem is that only current_qty is getting the right value (the other one gets always zero).

Example:

I create a lot. When I do this, current_qty is computed and values 10.000, and expected_qty is computed too and values 5.000.

Now I open the Transient Model, and current_qty gets 10.000, which is right, but expected_qty gets False. Why?

Even I have added other field to the Transient Model. This one is a computed char which should show the next: current_qty (expected_qty), following the values of this example, it should be 10.000 (5.000). But it also takes False value (it does not write the current_qty neither). Besides, I write log messages inside the compute method to check if the method is called and which values it takes. And surprisingly, the values of current_qty and expected_qty shown in the log are right! (10.000 and 5.000).

Python code

class ProductLotAvailable(models.TransientModel):
    _name = 'product.lot.available'

    @api.multi
    @api.depends('current_qty', 'expected_qty')
    def _compute_beautiful_qty(self):
        for available_lot in self:
            current_qty = str(available_lot.current_qty)
            _logger.info(current_qty)
            expected_qty = str(available_lot.expected_qty)
            _logger.info(expected_qty)
            available_lot.beautiful_qty = current_qty + '(' + expected_qty + ')'

    lot_id = fields.Many2one(
        comodel_name='stock.production.lot',
        string='Lot',
        readonly=True,
    )
    current_qty = fields.Float(
        related='lot_id.current_qty',
        string='Current lot quantity',
        readonly=True,
        digits=dp.get_precision('Product Unit of Measure'),
    )
    expected_qty = fields.Float(
        related='lot_id.expected_qty',
        string='Expected lot quantity',
        readonly=True,
        digits=dp.get_precision('Product Unit of Measure'),
    )
    beautiful_qty = fields.Char(
        compute='_compute_beautiful_qty',
        string='Current lot quantity',
        readonly=True,
    )

XML code

<field name="product_lots_available" nolabel="1">
    <tree create="false" delete="false" editable="bottom">
        <field name="current_qty" invisible="0"/>
        <field name="expected_qty" invisible="0"/>
        <field name="beautiful_qty"/>
        <field name="lot_id"/>
    </tree>
</field>

Can anyone help me with this? It is possible that there is a simple error in my code which I was not able to see for the last hours.


Solution

  • I found the problem: I had another module installed which was overwriting the method which loads the default values for the Transient Model. This method was filling in the current_qty, but not the expected_qty. And same problem with beautiful_qty.

    So despite I was introducing the values I needed, they were being overwritten by the other module.