2017-03-01 47 views
0

我有这样的模式:创建库存从继承的模型,操作按钮采摘 - Odoo V9社区

class fleet_vehicles_services(models.Model): 

@api.model 
def create(self, vals): 
    if vals.get('name', 'Nuevo') == 'Nuevo': 
     vals['name'] = self.env['ir.sequence'].next_by_code('fleet.vehicle.log.services') or '/' 
    return super(fleet_vehicles_services, self).create(vals) 

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

_inherit = "fleet.vehicle.log.services" 

name = fields.Char('Referencia de Orden', required=True, index=True, copy=False, readonly='True', default='Nuevo') 

x_location_src_id = fields.Many2one('stock.location', string=u'Ubicacion Origen de Productos', required=True, 
            readonly=False, 
            help="Location where the system will look for components.") 
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.") 
product_id = fields.Many2one('product.template', "Producto") 
product_uom_qty = fields.Float(string='Quantity', digits=dp.get_precision('Product Unit of Measure'), required=True, default=1.0) 
stock_picking = fields.Many2one("stock.picking", "Picking", required=True) 
state = fields.Selection(string="Estados", store=True, readonly=True, related="stock_picking.state") 

我想从它创建一个stock.picking,我已经有一个方法,这个类:

@api.multi 
def create_picking(self,vals): 
    vals = {'x_location_src_id': self, 'x_location_dest_id':self, 'product_id':self, 'product_uom_qty':self} 
    res = super(stock_picking, self).create(vals) 
    return res 

这是我从视图拨打:

<record model='ir.ui.view' id='fleet_vehicle_log_services_form_inherit'> 
     <field name='name'>fleet.vehicle.log.services.form</field> 
     <field name='model'>fleet.vehicle.log.services</field> 
     <field name='inherit_id' ref='fleet.fleet_vehicle_log_services_form'/> 
     <field name="priority">90</field> 
     <field name='arch' type='xml'> 
      <xpath expr="//form//sheet//group[1]" position="before"> 
       <div class="oe_title"> 
      <label for="name" class="oe_edit_only" /> 
      <h1> 
       <field name="name" /> 
      </h1> 
      </div> 
      </xpath> 
      <xpath expr="//form/sheet/group" position="after"> 
       <group string="Ubicaciones de Productos" col="2"> 
       </group> 
       <group><field name="stock_picking"/></group> 
       <group string="Datos del picking"> 
        <button name="create_picking" string="Crear Picking" type="object" class="oe_highlight"/> 
         <tree decoration-info="state == 'draft'" decoration-muted="state in ('cancel','done')" decoration-danger="state in ('confirmed','waiting')" string="Products to Consume"> 
          <field name="product_id"/> 
          <field name="product_uom_qty"/> 
          <field name="x_location_src_id"/> 
          <field name="x_location_dest_id"/> 
          <field name="state" invisible="1"/> 
         </tree> 
        </group> 
      </xpath> 
     </field> 
    </record> 

但每次我尝试点击该按钮时:

Traceback (most recent call last): 
File "/home/kristian/odoov9/odoo-9.0c-20161106/openerp/http.py", line 648, in _handle_exception 
return super(JsonRequest, self)._handle_exception(exception) 
File "/home/kristian/odoov9/odoo-9.0c-20161106/openerp/http.py", line 685, in dispatch 
result = self._call_function(**self.params) 
File "/home/kristian/odoov9/odoo-9.0c-20161106/openerp/http.py", line 321, in _call_function 
return checked_call(self.db, *args, **kwargs) 
File "/home/kristian/odoov9/odoo-9.0c-20161106/openerp/service/model.py", line 118, in wrapper 
return f(dbname, *args, **kwargs) 
File "/home/kristian/odoov9/odoo-9.0c-20161106/openerp/http.py", line 314, in checked_call 
result = self.endpoint(*a, **kw) 
File "/home/kristian/odoov9/odoo-9.0c-20161106/openerp/http.py", line 964, in __call__ 
return self.method(*args, **kw) 
File "/home/kristian/odoov9/odoo-9.0c-20161106/openerp/http.py", line 514, in response_wrap 
response = f(*args, **kw) 
File "/home/kristian/odoov9/odoo-9.0c-20161106/openerp/addons/web/controllers/main.py", line 892, in call_button 
action = self._call_kw(model, method, args, {}) 
File "/home/kristian/odoov9/odoo-9.0c-20161106/openerp/addons/web/controllers/main.py", line 880, in _call_kw 
return getattr(request.registry.get(model), method)(request.cr, request.uid, *args, **kwargs) 
File "/home/kristian/odoov9/odoo-9.0c-20161106/openerp/api.py", line 250, in wrapper 
return old_api(self, *args, **kwargs) 
File "/home/kristian/odoov9/odoo-9.0c-20161106/openerp/api.py", line 354, in old_api 
result = method(recs, *args, **kwargs) 
File "/home/kristian/odoov9/danisan/fleet_stock/models/fleet_vehicle_services.py", line 218, in create_picking 
res = super(stock_picking, self).create(vals) 
TypeError: super(type, obj): obj must be an instance or subtype of type 

对此的任何想法?

在此先感谢。

回答

1

这里您不需要super(),因为您想要创建另一个odoo模型数据库记录。要做到这一点,请使用Environment,其中所有型号都已注册并致电创建。我也不相信selfself.idvals中的字段的正确值。

@api.multi 
def create_picking(self): 
    self.ensure_one() 
    vals = { 
     'x_location_src_id': self.x_location_src_id.id, 
     'x_location_dest_id': self.x_location_dest_id.id, 
     'product_id': self.product_id.id, # shouldn't be set on stock.picking, products are handled on it's positions (stock.move) 
     'product_uom_qty': self.product_uom_qty # the same as for product_id 
    } 
    picking = self.env['stock.picking'].create(vals) 
    return picking 
+0

是的,错误仍然存​​在,要这样试试 – NeoVe

+0

还有一个错误,我会为此打开一个新问题,谢谢您 – NeoVe

1

我到目前为止注意到的是:create正在等待vals字典中的确切值。 Many2one是什么? (这就是为什么你需要在开始开发模块之前阅读odoo文档)Many2one只是存储相关模型ID的数据库中的一列。因为它存储ID它存储整数值,但在你的情况下,你给它的对象self哪些create不能理解。所以你必须使用self.id

注意:在正常的模型函数中,您可以使用self将值赋给Many2one字段,但是在创建给定值字典时您必须指定确切值,因为odoo不会为您计算它。

@api.multi 
def create_picking(self,vals): 
    vals = { 
     'x_location_src_id': self.id, 
     'x_location_dest_id': self.id, 
     'product_id': self.id, 
     'product_uom_qty': self.id 
    } 
    res = super(stock_picking, self).create(vals) 
    return res 

而且最后通知:使用PEP8

代码必须易于读取!