Inherit field from one model to another model - Odoo v9 Community
<p>I'm trying to add a field from a table, into another table, through a module.</p>

<p>Specifically, trying to inherit a field from <code>product.product</code>, the <code>price</code> field, to add it into <code>stock.move</code> model.</p>

<p>So, I've created a model into this new module I'm making.</p>

<p>Like this:</p>

<pre><code># -*- 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)
</code></pre>

<p>Then, on my views:</p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; 
&lt;openerp&gt;
&lt;data&gt;

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

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

&lt;/data&gt;
&lt;/openerp&gt;
</code></pre>

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

<p>So, I don't really know what's wrong with it.</p>

<p>Any ideas on how to accomplish this?</p>

<p>Thanks in advance!</p>

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

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

<pre><code>class StockMove(models.Model):
    _inherit = 'stock.move'

    price_unity = fields.Many2one("product.product", string="Precio", readonly=True)
</code></pre>

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

<pre><code>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")
</code></pre>

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

python openerp odoo-9 qweb