自动创建 stock.location - Odoo v9 社区

Create stock.location automatically - Odoo v9 community

我有这个方法,它继承自模块 fleetfleet.vehicle 模型上的 stock.location

class fleet_vehicle(models.Model):
    _inherit = 'fleet.vehicle'

    location_id = fields.Many2one("stock.location", string="Almacén Origen", store=True)

所以,这个grings模型stock.location变成了fleet.vehicle模型,这是我的看法:

    <record model='ir.ui.view' id='fleet_vehicle_form'>
        <field name="name">fleet.vehicle.form</field>
        <field name="model">fleet.vehicle</field>
        <field name="inherit_id" ref='fleet.fleet_vehicle_form'/>
        <field name="arch" type="xml">
            <xpath expr="//form/sheet/group" position="after">
                <group string="Almacén correspondiente" col="2">
                <field name="location_id"/>
                </group>
            </xpath>
        </field>
    </record>

到目前为止,一切顺利,现在,每次将新车辆添加到数据库时,我都被要求创建一个位置,来自 fleet.vehicle 表格。

如您所见,我已经添加了模型和字段以添加位置,但是,如何在每次保存新车辆时都创建此位置?

例如,我创建了车辆 Opel/Astra,这应该在系统中自动创建一个名为 Opel/Astra 的新位置,我知道如何声明默认位置、源位置和目标位置,因为像这样的例子:

def _static_location(self):
    return self.env.ref('fleet_stock.location_stock')

然后从字段调用函数,如:

    x_location_dest_id = fields.Many2one('stock.location', string=u'Ubicacion Destino de Productos', required=True,
                                    readonly=False, default=_static_location,
                                    help="Location where the system will look for components.")

这当然是在 data 文件夹中的 xml 文件中声明的,例如:

<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data noupdate="1">
<record id="location_stock" model="stock.location">
    <field name="name">ReparacionUnidades</field>
    <field name="location_id" ref="stock.stock_location_locations_virtual"/>
    <field name="usage">production</field>
    <field name="company_id"></field>
</record>
</data>
</openerp>

现在,我如何才能 "default" 这种行为,而不是到一个已经存在的位置,而是根据 fleet.vehicle 汽车名称创建一个新位置?

我猜,您需要根据汽车名称创建新的 stock.location。 但是我很困惑你为什么在你的例子中使用数据文件。

你必须做的:搜索已经有 fleet.vehicle 名称的 stock.location。如果搜索成功设置location_id。如果没有创建新的 stock.location 并分配给 location_id。我看不出有什么问题,为什么你自己做不到?

因为我不知道你的系统是如何工作的,所以我不能给你代码。但是您需要 location_id 上的 onchange 方法。如果设置了名称,那么您可以去搜索 stock.location 或创建并分配新名称。