将字段从一种模型继承到另一种模型-Odoo v9社区 [英] Inherit field from one model to another model - Odoo v9 Community

查看:188
本文介绍了将字段从一种模型继承到另一种模型-Odoo v9社区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过模块将一个表中的字段添加到另一个表中.

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

特别是,尝试从price字段product.product继承一个字段,以将其添加到stock.move模型中.

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.

赞:

# -*- 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)

然后,根据我的观点:

<?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?

提前谢谢!

推荐答案

您遇到的问题是,您继承了product.product,并使用One2many字段再次链接回它.

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

如果要将产品价格添加到stock.move,只需删除扩展product.product的其他模型并像在stock.move模型中所做的那样建立Many2one链接,除了模型名称为product.product

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")

注意:您不需要product_id(stock.move模型已经具有指向具有相同名称的product.product的链接),我只是将其放在此处以显示相关字段的工作方式

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

这篇关于将字段从一种模型继承到另一种模型-Odoo v9社区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆