NeoVe NeoVe - 1 month ago 25
Python Question

Call field from inherited model Odoo v9 community

Let me explain what I'm trying to do.

On

stock
module, when You navigate to whatever operation (picking) type, being it a tree or form view, some of the fields that are shown are from
product.product
, like, for example
name
.

Now, the model
product.product
has a field called
price
, so, I need to show product price on
stock.picking
movements.

Since the
stock.picking
model doesn't inherit the
price
field, I'm creating a little module, to inherit
price
from
product.product
, and then show it on
stock.picking
.

It is a third module beside
stock
and
product
.

Now, in my
models.py
I declare:

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

from openerp import models, fields, api

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

@api.onchange('name','product_id','move_line_tax_ids','product_uom_qty')

price_unit = fields.Float( digits_compute=dp.get_precision('Product Price'), string='Price')


In my
view.xml
I add this field to
stock.picking
:

<?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.picking</field>
<field name="inherit_id" ref="stock.vpicktree"/>
<field name="arch" type="xml">
<field name="state" position="before">
<field name="price_unit"/>
</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.picking</field>
<field name="inherit_id" ref="stock.view_picking_form"/>
<field name="arch" type="xml">
<field name="state" position="before">
<field name="price_unit"/>
</field>
</field>
</record>

</data>
</openerp>


Every time I try to run this, it throws me on console:

2016-10-15 05:23:44,821 21578 ERROR argentina werkzeug: Error on request:
Traceback (most recent call last):
File "/home/kristian/.virtualenvs/odoo_danisan/lib/python2.7/site-packages/werkzeug/serving.py", line 177, in run_wsgi
execute(self.server.app)
File "/home/kristian/.virtualenvs/odoo_danisan/lib/python2.7/site-packages/werkzeug/serving.py", line 165, in execute
application_iter = app(environ, start_response)
File "/home/kristian/odoov9/odoo-9.0/openerp/service/server.py", line 246, in app
return self.app(e, s)
File "/home/kristian/odoov9/odoo-9.0/openerp/service/wsgi_server.py", line 184, in application
return application_unproxied(environ, start_response)
File "/home/kristian/odoov9/odoo-9.0/openerp/service/wsgi_server.py", line 170, in application_unproxied
result = handler(environ, start_response)
File "/home/kristian/odoov9/odoo-9.0/openerp/http.py", line 1495, in __call__
return self.dispatch(environ, start_response)
File "/home/kristian/odoov9/odoo-9.0/openerp/http.py", line 1644, in dispatch
ir_http = request.registry['ir.http']
File "/home/kristian/odoov9/odoo-9.0/openerp/http.py", line 365, in registry
return openerp.modules.registry.RegistryManager.get(self.db) if self.db else None
File "/home/kristian/odoov9/odoo-9.0/openerp/modules/registry.py", line 355, in get
update_module)
File "/home/kristian/odoov9/odoo-9.0/openerp/modules/registry.py", line 386, in new
openerp.modules.load_modules(registry._db, force_demo, status, update_module)
File "/home/kristian/odoov9/odoo-9.0/openerp/modules/loading.py", line 334, in load_modules
force, status, report, loaded_modules, update_module)
File "/home/kristian/odoov9/odoo-9.0/openerp/modules/loading.py", line 237, in load_marked_modules
loaded, processed = load_module_graph(cr, graph, progressdict, report=report, skip_modules=loaded_modules, perform_checks=perform_checks)
File "/home/kristian/odoov9/odoo-9.0/openerp/modules/loading.py", line 123, in load_module_graph
load_openerp_module(package.name)
File "/home/kristian/odoov9/odoo-9.0/openerp/modules/module.py", line 331, in load_openerp_module
__import__('openerp.addons.' + module_name)
File "/home/kristian/odoov9/odoo-9.0/openerp/modules/module.py", line 61, in load_module
mod = imp.load_module('openerp.addons.' + module_part, f, path, descr)
File "/home/kristian/odoov9/motostion_addons/stock_move_price/__init__.py", line 7, in <module>
from . import models
File "/home/kristian/odoov9/motostion_addons/stock_move_price/models/__init__.py", line 1, in <module>
from . import models
File "/home/kristian/odoov9/motostion_addons/stock_move_price/models/models.py", line 10
price_unit = fields.Float( digits_compute=dp.get_precision('Product Price'), string='Price')
^
SyntaxError: invalid syntax


Any ideas on how can I achieve this?

I hope I've explained myself, if You need further explanation just ask me please.

Thanks in advance!

Answer

Try with following code:

Replace

price_unit = fields.Float( digits_compute=dp.get_precision('Product Price'), string='Price')

with

price_unit = fields.Float(string='Price', digits=dp.get_precision('Product Price'))

And write below line in import section

import openerp.addons.decimal_precision as dp

Afterwards restart Odoo server and upgrade your custom module.

EDIT:

Replace

_inherit = 'stock.move'

with

_inherit = 'stock.picking'