如何解决这个错误:'product.template'没有属性'generate_ean13'?

时间:2018-05-07 16:19:17

标签: python xml python-2.7 odoo odoo-9

我正在使用Odoo 9,我已经为产品条形码生成创建了一个自定义模块。我在产品模板表单中创建了一个自定义按钮,生成条形码并将其插入字段barcode。但每次按“生成ean13”按钮时,此错误显示,我不知道该怎么做。这是我的代码:

product_view.xml

<odoo>
    <record id="view_product_category_simple_ean_form" model="ir.ui.view">
        <field name="name">product.category.form</field>
        <field name="model">product.category</field>
        <field name="inherit_id" ref="product.product_category_form_view" />
        <field name="arch" type="xml">
            <field name="type" position="after">
                <field name="ean_sequence_id" domain="[('barcode_sequence', '=', 1)]"
                    context="{'default_name': name and 'EAN ' + name or 'EAN', 'default_barcode_sequence': 1}" />
                <newline />
            </field>
        </field>
    </record>

    <record id="view_product_simple_ean_form" model="ir.ui.view">
        <field name="name">product.template.product.form.barcode</field>
        <field name="model">product.template</field>
        <field name="inherit_id" ref="product.product_template_only_form_view" />
        <field name="arch" type="xml">
            <field name="barcode" position="after">    
                <button string="Generate the EAN13" name="generate_ean13"
                    attrs="{'invisible': [('barcode', '!=', False)]}"
                    type="object" colspan="2" />
            </field>
        </field>
    </record>
</odoo>

product.py

from openerp import api, models, fields, _
from openerp  import exceptions


def isodd(x):
    return bool(x % 2)


class ProductCategory(models.Model):
    _inherit = 'product.category'

    ean_sequence_id = fields.Many2one('ir.sequence', string='Ean sequence')


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

    barcode = fields.Char(copy=False)
    ean= fields.Char(copy=False)

    _columns = {
        ean_sequence_id: fields.Many2one('ir.sequence', string='Ean sequence')
    }


    @api.model
    def _get_ean_next_code(self, product):
        sequence_obj = self.env['ir.sequence']
        if product.ean_sequence_id:
            ean = sequence_obj.next_by_id(product.ean_sequence_id.id)
        elif product.categ_id.ean_sequence_id:
            ean = sequence_obj.next_by_id(product.categ_id.ean_sequence_id.id)
        elif product.company_id and product.company_id.ean_sequence_id:
            ean = sequence_obj.next_by_id(
                product.company_id.ean_sequence_id.id)
        elif self.env.context.get('sequence_id', False):
            ean = sequence_obj.next_by_id(self.env.context.get('sequence_id'))
        else:
            return None
        ean = (len(ean[0:6]) == 6 and ean[0:6] or
            ean[0:6].ljust(6, '0')) + ean[6:].rjust(6, '0')
        if len(ean) > 12:
            raise exceptions.Warning(
                _("Configuration Error!"
                "The next sequence is longer than 12 characters. "
                "It is not valid for an EAN13 needing 12 characters, "
                "the 13 being used as a control digit"
                "You will have to redefine the sequence or create a new one")
                )

        return ean

    def _get_ean_control_digit(self, code):
        sum = 0
        for i in range(12):
            if isodd(i):
                sum += 3 * int(code[i])
            else:
                sum += int(code[i])
        key = (10 - sum % 10) % 10
        return '%d' % key

    @api.model
    def _generate_ean13_value(self, product):
        ean = self._get_ean_next_code(product)
        if not ean:
            return None
        key = self._get_ean_control_digit(ean)
        barcode = ean + key
        return barcode

    @api.one
    def generate_ean13(self):
        if not self.barcode:
            barcode = self._generate_ean13_value(self)
            if barcode:
                self.write({'barcode': barcode})

    return True

我的追溯

Traceback (most recent call last):
File "D:\Projet_Odoo\Odoo 9.0-20180426\server\.\openerp\http.py", line 650, 
in _handle_exception
File "D:\Projet_Odoo\Odoo 9.0-20180426\server\.\openerp\http.py", line 687, 
in dispatch
File "D:\Projet_Odoo\Odoo 9.0-20180426\server\.\openerp\http.py", line 323, 
in _call_function
File "D:\Projet_Odoo\Odoo 9.0-20180426\server\.\openerp\service\model.py", 
line 118, in wrapper
File "D:\Projet_Odoo\Odoo 9.0-20180426\server\.\openerp\http.py", line 316, 
in checked_call
File "D:\Projet_Odoo\Odoo 9.0-20180426\server\.\openerp\http.py", line 966, 
in __call__
File "D:\Projet_Odoo\Odoo 9.0-20180426\server\.\openerp\http.py", line 516, 
in response_wrap
File "D:\Projet_Odoo\Odoo 9.0- 
20180426\server\openerp\addons\web\controllers\main.py", line 900, in 
call_button
File "D:\Projet_Odoo\Odoo 9.0- 
20180426\server\openerp\addons\web\controllers\main.py", line 888, in 
_call_kw
 AttributeError: 'product.template' object has no attribute 'generate_ean13'

1 个答案:

答案 0 :(得分:1)

我看到了几个错误

首先,不要使用_columns字典,这在旧的api中使用。所以声明字段ean_sequence_id是这样的:

ean_sequence_id = fields.Many2one(
    string='Ean sequence',
    comodel_name='ir.sequence',
)

按钮调用的方法应该有一个@api.multi装饰器:

@api.multi
def generate_ean13(self):
    for record in self:
        if not record.barcode:
            barcode = record._generate_ean13_value(record)
            if barcode:
                record.write({'barcode': barcode})