如何使用 odoo 8.0 中的上下文默认在销售订单行中显示销售订单字段?
How to show sales order field in the sales orderline by default using context in odoo 8.0?
我在 sale.order 模型下有一个名为 sales_ship 的字段,在 sale.order.line 中有另一个名为 ship_sale 的字段。现在我想要的是让 sales.order 中选择的 sales_ship 默认出现在 sale.order.line 的 ship_sale 中。如何使用上下文来做到这一点?这是我的完整代码。
py.
from openerp import models , fields
class ShipInfo (models.Model):
_name = "ship.ship"
imo = fields.Char (string = "IMO" , size = 64 , required = True )
hull_number = fields.Char (string = ' Hull Number ' , size = 64 , required = True )
engine_number = fields.Char ( string = ' Engine Number ' , size = 64 , required = True )
vessel_name = fields.Char ( string = 'Vessel Name' , size = 64 )
build_year = fields.Char ( string = 'Build Year' , size = 64 )
ship_yard = fields.Many2one ( 'res.partner' , string = 'Ship Yard')
ship_owner = fields.Many2one ( 'res.partner' , string = 'Ship Owner' )
ship_management = fields.Many2one ( 'res.partner' ,string= 'Ship Management' )
engine_builder = fields.Many2one ( 'res.partner' , string = 'Engine Builder' )
_rec_name = 'vessel_name'
class SaleOrderInherited (models.Model):
_inherit = 'sale.order'
sales_ship = fields.Many2one ( 'ship.ship' , string = 'Sales Ship' )
class SaleOrderLineInherited (models.Model):
_inherit = 'sale.order.line'
ship_sale = fields.Many2one ( 'ship.ship' , string = 'Sales Ship')
.xml
<?xml version="1.0" encoding="UTF-8"?>
<openerp>
<data>
<menuitem id="ship_module" name="ship" parent="base.menu_base_config" action="action_view_ship" sequence="30"/>
<!--<menuitem id="ship_menu" name="ship information" parent="ship_module"/>-->
<record model="ir.ui.view" id="ship_form">
<field name="name">ship.ship.form.view</field>
<field name="model">ship.ship</field>
<field name="type">form</field>
<field name="arch" type="xml">
<form string="ship info">
<sheet>
<group>
<group> <field name="imo"/>
<field name="hull_number"/>
<field name="engine_number"/>
<field name="vessel_name" context="{'sales_ship':sales_ship}"/>
<field name="build_year"/>
</group>
<group>
<field name="ship_yard"/>
<field name="ship_owner"/>
<field name="ship_management"/>
<field name="engine_builder"/>
</group>
</group>
</sheet>
</form>
</field>
</record>
<record model="ir.ui.view" id="ship_tree">
<field name="name">ship.ship.tree</field>
<field name="model">ship.ship</field>
<field name="type">tree</field>
<field name="arch" type="xml">
<tree string="ship info">
<field name="imo"/>
<field name="hull_number"/>
<field name="engine_number"/>
<field name="vessel_name" context="{'sales_ship':sales_ship}"/>
<field name="build_year"/>
<field name="ship_yard"/>
<field name="ship_owner"/>
<field name="ship_management"/>
<field name="engine_builder"/>
</tree>
</field>
</record>
<record model="ir.actions.act_window" id="action_view_ship">
<field name="name">ship info</field>
<field name="res_model">ship.ship</field>
<field name="view_type">form</field>
<field name="view_mode">tree,form</field>
<field name="help" type="html">
<p class="oe_view_nocontent_create">Create new ship
</p>
</field>
</record>
<!--Inherit the sale order form view-->
<record id="view_sale_order_custom" model="ir.ui.view">
<field name="name">sale.order.custom.form.inherited</field>
<field name="model">sale.order</field>
<field name="inherit_id" ref="sale.view_order_form"/>
<field name="arch" type="xml">
<xpath expr="//field[@name='partner_id']" position="after">
<field name="sales_ship" context="{'sales_ship':sales_ship}"/>
</xpath>
<xpath expr="/form/sheet/notebook/page/field[@name='order_line']/tree/field[@name='name']" position="after">
<field name="ship_sale" context="{'sales_ship':sales_ship}"/>
</xpath>
<group>
<button string="Add to Order Line"/>
</group>
</field>
</record>
</data>
<record id="view_sale_order_custom" model="ir.ui.view">
<field name="name">sale.order.custom.form.inherited</field>
<field name="model">sale.order</field>
<field name="inherit_id" ref="sale.view_order_form"/>
<field name="arch" type="xml">
<xpath expr="//field[@name='partner_id']" position="after">
<field name="sales_ship" />
</xpath>
<xpath expr="/form/sheet/notebook/page/field[@name='order_line']" position="attributes">
<attribute name="context">{'default_sales_ship':sales_ship}</attribute>
</xpath>
<xpath expr="/form/sheet/notebook/page/field[@name='order_line']/tree/field[@name='name']" position="after">
<field name="ship_sale" />
</xpath>
<group>
<button string="Add to Order Line"/>
</group>
</field>
</record>
我在 sale.order 模型下有一个名为 sales_ship 的字段,在 sale.order.line 中有另一个名为 ship_sale 的字段。现在我想要的是让 sales.order 中选择的 sales_ship 默认出现在 sale.order.line 的 ship_sale 中。如何使用上下文来做到这一点?这是我的完整代码。
py.
from openerp import models , fields
class ShipInfo (models.Model):
_name = "ship.ship"
imo = fields.Char (string = "IMO" , size = 64 , required = True )
hull_number = fields.Char (string = ' Hull Number ' , size = 64 , required = True )
engine_number = fields.Char ( string = ' Engine Number ' , size = 64 , required = True )
vessel_name = fields.Char ( string = 'Vessel Name' , size = 64 )
build_year = fields.Char ( string = 'Build Year' , size = 64 )
ship_yard = fields.Many2one ( 'res.partner' , string = 'Ship Yard')
ship_owner = fields.Many2one ( 'res.partner' , string = 'Ship Owner' )
ship_management = fields.Many2one ( 'res.partner' ,string= 'Ship Management' )
engine_builder = fields.Many2one ( 'res.partner' , string = 'Engine Builder' )
_rec_name = 'vessel_name'
class SaleOrderInherited (models.Model):
_inherit = 'sale.order'
sales_ship = fields.Many2one ( 'ship.ship' , string = 'Sales Ship' )
class SaleOrderLineInherited (models.Model):
_inherit = 'sale.order.line'
ship_sale = fields.Many2one ( 'ship.ship' , string = 'Sales Ship')
.xml
<?xml version="1.0" encoding="UTF-8"?>
<openerp>
<data>
<menuitem id="ship_module" name="ship" parent="base.menu_base_config" action="action_view_ship" sequence="30"/>
<!--<menuitem id="ship_menu" name="ship information" parent="ship_module"/>-->
<record model="ir.ui.view" id="ship_form">
<field name="name">ship.ship.form.view</field>
<field name="model">ship.ship</field>
<field name="type">form</field>
<field name="arch" type="xml">
<form string="ship info">
<sheet>
<group>
<group> <field name="imo"/>
<field name="hull_number"/>
<field name="engine_number"/>
<field name="vessel_name" context="{'sales_ship':sales_ship}"/>
<field name="build_year"/>
</group>
<group>
<field name="ship_yard"/>
<field name="ship_owner"/>
<field name="ship_management"/>
<field name="engine_builder"/>
</group>
</group>
</sheet>
</form>
</field>
</record>
<record model="ir.ui.view" id="ship_tree">
<field name="name">ship.ship.tree</field>
<field name="model">ship.ship</field>
<field name="type">tree</field>
<field name="arch" type="xml">
<tree string="ship info">
<field name="imo"/>
<field name="hull_number"/>
<field name="engine_number"/>
<field name="vessel_name" context="{'sales_ship':sales_ship}"/>
<field name="build_year"/>
<field name="ship_yard"/>
<field name="ship_owner"/>
<field name="ship_management"/>
<field name="engine_builder"/>
</tree>
</field>
</record>
<record model="ir.actions.act_window" id="action_view_ship">
<field name="name">ship info</field>
<field name="res_model">ship.ship</field>
<field name="view_type">form</field>
<field name="view_mode">tree,form</field>
<field name="help" type="html">
<p class="oe_view_nocontent_create">Create new ship
</p>
</field>
</record>
<!--Inherit the sale order form view-->
<record id="view_sale_order_custom" model="ir.ui.view">
<field name="name">sale.order.custom.form.inherited</field>
<field name="model">sale.order</field>
<field name="inherit_id" ref="sale.view_order_form"/>
<field name="arch" type="xml">
<xpath expr="//field[@name='partner_id']" position="after">
<field name="sales_ship" context="{'sales_ship':sales_ship}"/>
</xpath>
<xpath expr="/form/sheet/notebook/page/field[@name='order_line']/tree/field[@name='name']" position="after">
<field name="ship_sale" context="{'sales_ship':sales_ship}"/>
</xpath>
<group>
<button string="Add to Order Line"/>
</group>
</field>
</record>
</data>
<record id="view_sale_order_custom" model="ir.ui.view">
<field name="name">sale.order.custom.form.inherited</field>
<field name="model">sale.order</field>
<field name="inherit_id" ref="sale.view_order_form"/>
<field name="arch" type="xml">
<xpath expr="//field[@name='partner_id']" position="after">
<field name="sales_ship" />
</xpath>
<xpath expr="/form/sheet/notebook/page/field[@name='order_line']" position="attributes">
<attribute name="context">{'default_sales_ship':sales_ship}</attribute>
</xpath>
<xpath expr="/form/sheet/notebook/page/field[@name='order_line']/tree/field[@name='name']" position="after">
<field name="ship_sale" />
</xpath>
<group>
<button string="Add to Order Line"/>
</group>
</field>
</record>