我有这个方法,它继承自模块fleet的fleet.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>现在,我如何才能将这个行为“默认”到一个已经存在的位置,而是根据fleet.vehicle汽车的名称创建一个新的位置呢?
发布于 2017-03-01 15:37:20
如我所料,您需要根据汽车名称创建新的stock.location。但是我不明白你为什么在你的例子中使用数据文件。
您必须做的:搜索已经具有fleet.vehicle名称的stock.location。如果搜索成功,则设置location_id。如果没有,则创建新的stock.location并分配给location_id。我看不出有什么问题,为什么你不能自己做呢?
因为我不知道你的系统是怎么工作的,所以我不能给你代码。但您需要在location_id上使用onchange方法。如果设置了name,那么您可以搜索stock.location或创建并分配新的名称。
https://stackoverflow.com/questions/42522489
复制相似问题