简体   繁体   中英

Odoo 11 rewrite copy function to add extra fields

I made custom modules that add fields to the product.template form view (products in the inventory, sales, warehouse) and those fields are not been duplicated with the product when I try to. Therefore, I'm trying to inherit and rewrite the function copy of that module .

My code:

class PurchaseOrder(models.Model):
    _inherit= 'product.template'

    @api.onchange('attribute_set_id')
    def _onchange_attributes(self):
       # SOME CODE HERE

    @api.one
    def copy(self, default=None):
        default = dict(default or {})
        default.update({
            'weight': float(43),
        })
        return super(ProductTemplate, self).copy(default)

These gives me the error:

  ...
  File "/var/www/.local/share/Odoo/addons/11.0/sync2ba2/models/sync2ba_mob.py", line 89, in copy
    return super(ProductTemplate, self).copy(default)
NameError: name 'ProductTemplate' is not defined

I also tried with different names and types like product.template, product_template, 'product.template', [product.template].

I tried changing the name of the class to ProductTemplate and I get the error:

  ...
  File "/usr/local/lib/python3.5/dist-packages/odoo-11.0+e.20171006-py3.5.egg/odoo/models.py", line 3833, in <lambda>
    @api.returns('self', lambda value: value.id)
AttributeError: 'list' object has no attribute 'id'

I'm not sure if that error is from the copy function or something else. Either way I would like some help figuring out what I'm doing wrong. This is Odoo 11 enterprise. Thank you.

Change your class name to ProductTemplate and use @api.multi instead of @api.one

class ProductTemplate(models.Model):
      _inherit= 'product.template'

      @api.onchange('attribute_set_id')
      def _onchange_attributes(self):
         # SOME CODE HERE

      @api.multi
      def copy(self, default=None):
          default = dict(default or {})
          default.update({'weight': float(43)})
          return super(ProductTemplate, self).copy(default)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM