NeoVe
NeoVe

Reputation: 3907

Inherit field from one model to another model - Odoo v9 Community

I'm trying to add a field from a table, into another table, through a module.

Specifically, trying to inherit a field from product.product, the price field, to add it into stock.move model.

So, I've created a model into this new module I'm making.

Like this:

# -*- coding: utf-8 -*-

from openerp import models, fields, api
import openerp.addons.decimal_precision as dp 

class product(models.Model):
    _inherit = 'product.product'
    _rec_name = 'price_unidad'

    price_unidad = fields.One2many('product.product','price', string="Precio", readonly=True)

 class StockMove(models.Model):
    _inherit = 'stock.move'

    price_unity = fields.Many2one("product", string="Precio", readonly=True)

Then, on my views:

<?xml version="1.0" encoding="utf-8"?> 
<openerp>
<data>

<record id="view_stock_move_tree" model="ir.ui.view">
    <field name="name">Stock Move Price Tree</field>
    <field name="model">stock.move</field>
    <field name="inherit_id" ref="stock.view_move_picking_tree"/>
    <field name="arch" type="xml">
        <field name="state" position="before">
            <field name="price_unity"/>
        </field>
    </field> 
</record>

<record id="view_stock_move_form" model="ir.ui.view">
    <field name="name">Stock Move Price Form</field>
    <field name="model">stock.move</field>
    <field name="inherit_id" ref="stock.view_move_picking_form"/>
    <field name="arch" type="xml">
        <field name="state" position="before">
                <field name="price_unity"/>
            </field>
    </field>
</record>

</data>
</openerp>

I'm not really sure, but it seems like it enters into a neverending loop when I call it from the form view.

So, I don't really know what's wrong with it.

Any ideas on how to accomplish this?

Thanks in advance!

Upvotes: 1

Views: 5001

Answers (2)

danidee
danidee

Reputation: 9634

The problem you have is that you're inheriting product.product and linking back to it again with a One2many field

If you want to add the product price to stock.move just delete the extra model that extends product.product and make a Many2one link like you've done in your stock.move model except that the model name is product.product

class StockMove(models.Model):
    _inherit = 'stock.move'

    price_unity = fields.Many2one("product.product", string="Precio", readonly=True)

This picks the object as a whole, but if you want only the price, then you'll have to use a related field

class StockMove(models.Model):
    _inherit = 'stock.move'

    product_id = fields.Many2one("product.product", "Product")
    price_unity = fields.Float(string="Precio", store=True, readonly=True, related="product_id.price")

Note: you don't need the product_id (the stock.move model already has a link to product.product with the same name), i just put it there to show you how related fields work

Upvotes: 1

CZoellner
CZoellner

Reputation: 14801

What about a related field on stock.move?

class StockMove(models.Model):
    _inherit = "stock.move"

    price_unity = fields.Float(
        string="Precio", related="product_id.price", readonly=True)

Upvotes: 1

Related Questions