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

独自空忆成欢 提交于 2020-02-01 09:18:26

问题


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!


回答1:


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




回答2:


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)


来源:https://stackoverflow.com/questions/40099291/inherit-field-from-one-model-to-another-model-odoo-v9-community

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!