2017-03-01 64 views
0

我有这样的方法,它继承了stock.locationfleet.vehicle模型从模块fleet自动创建stock.location - Odoo V9社区

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

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

所以,这grings模型stock.locationfleet.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.") 
这当然是宣布一个XML文件中 data文件夹,像

<?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汽车名称创建一个新的?

回答

2

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

您需要做的是:搜索stock.location其中已有fleet.vehicle名称。如果搜索成功,则设置为location_id。如果不创建新的stock.location并分配给location_id。我没有在这里看到问题,为什么你不能自己做到这一点?

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

+0

嗨,对不起,这只是一个例子,是的,这样的事情是我需要的,但我不知道如何在v9 API上接近它,你能指点我一些例子吗? – NeoVe

+1

我相信你可以自己写。创建'@ api.onchange('car_name')'然后在函数中写入:'self.env ['stock.location']。create({'name':self.car_name})'你必须搜索它:'self.env ['stock.location']。search([('name','=',self.car_name)])'。我认为它非常微不足道 –

相关问题