This question has been flagged
2 Replies
4783 Views

Hello, I am developing a module in Odoo 8 and I have a very strange problem. I added a One2many field in product.pricelist.version model, and in account.voucher model I have the corresponding Many2one field.

class product_pricelist_version(models.Model):
    _inherit = 'product.pricelist.version'

    pays= fields.One2many('account.voucher', 'pricelist_version_id')

Then, I override the create method of product.pricelist.item model.

    def create(self, cr, uid, vals, context=None):

        id_pricelist = super(product_pricelist_item, self).create(cr, uid, vals, context)

        pricelist_item_obj = self.browse(cr, uid, id_pricelist, context)

        pricelist_version_obj = pricelist_item_obj.price_version_id

When I create a pricelist.version with one or more "pays", I do some logic. If I check the lenght with "len(pricelist_version_obj.pays)" I get 0! Why?! If I do "len(pricelist_item_obj.items_id)" I get the correct number of items...

Avatar
Discard
Best Answer

First, does account.voucher has pricelist_version_id?  If it is there, are there any account.voucher that has the that particular pricelist.version?

Avatar
Discard
Author Best Answer

Yes and yes. I think that the problem is because Odoo first creates the product.pricelist.item and then the product.pricelist.version. So, when I access to product.pricelist.version.pay in create method of product.pricelist.item the object are still empty... Is it possible?
 

Avatar
Discard

It is possible. However as long as the items are eventually connected to the version, it would calculate OK. Even in the create method like you have done (which is calling it after the super call). Check what is the value of pricelist_version_obj, is it referring to the correct product.pricelist.version (I would imagine so, because if it is empty, the len(pricelist_version_obj.pays) code would raise an exception).

Author

I think that Odoo calculates correctly the items lenght because the items are created yet, after the create method of product.pricelist.item. The pricelist is the correct, I check it with his attribute product.pricelist.name. I solved the problem doing the logic in the create method of product.pricelist.version, where the lenght of the "pays" attribute is the correct.