Skip to Content
Menu
This question has been flagged

I created a field of type Boolean 'check_barcode' in the stock.config.settings object. The intent of this field is as follows if it is marked as true it would have entered a method that is in another inherited object. The problem is that my self.env is not bringing the check_barcode field (I think it's not making the stay). 


How to call the check_barcode field in the def calculate_checksum method?


field.py

class StockSettings(models.TransientModel):

    _inherit = 'stock.config.settings'

    check_barcode = fields.Boolean(string='check_barcode')


product.py

class ProductTemplate(models.Model):

    _inherit = "product.template"

    @api.onchange('barcode')

    def calculate_checksum(self):

            #if self.env.user.company_id.product_ids.compute(self.company_id.check_barcode, self.product_ids):

            #if self.env['res.config.settings'].check_barcode:                                    #.company_id.product_ids.compute(self.company_id.check_barcode, self.product_ids):

        if self.check_barcode == 'True':

            if self.barcode:

                barcode = self.barcode[:12]

                if len(barcode) < 12:

                    barcode = barcode.ljust(12, '0')



                sum_ = lambda x, y: int(x) + int(y)

                evensum = reduce(sum_, barcode[::2])

                oddsum = reduce(sum_, barcode[1::2])

                digit = (10 - ((evensum + oddsum * 3) % 10)) % 10

                self.write(

                    {

                        'barcode': barcode + str(digit)

                    }

            )

            else:

                return False






Avatar
Discard
Best Answer

Hi,

You can update your field.py file like this,

class StockSettings(models.TransientModel):
_inherit = 'stock.config.settings'

check_barcode = fields.Boolean(string='check_barcode')

@api.multi
def set_check_barcode(self):
""" saving the values to ir.values """
IrValues = self.env['ir.values']
IrValues.set_default('stock.config.settings', 'check_barcode', self.check_barcode)


Then in the calculate_checksum function, you can get the value of the check_barcode like this,

ir_values = self.env['ir.values']
check_barcode = ir_values.get_default('stock.config.settings', 'check_barcode')


Now your code will be like this,

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

@api.onchange('barcode')
def calculate_checksum(self):
ir_values = self.env['ir.values']
check_barcode = ir_values.get_default('stock.config.settings', 'check_barcode')


Thanks

Avatar
Discard
Author

I made the update but it still gives error : [ in calculate_checksum

if self.check_barcode == 'True':

AttributeError: 'product.template' object has no attribute 'check_barcode' ]

Is this the correct syntax?

@api.onchange('barcode')

def calculate_checksum(self):

ir_values = self.env['ir.values']

check_barcode = ir_values.get_default('stock.config.settings', 'check_barcode')

if self.check_barcode == 'True':

In the product.template model there is no field check_barcode, so change self.check_barcode to self.barcode

Related Posts Replies Views Activity
2
Feb 23
11376
2
Jan 19
13209
4
Mar 24
1664
1
Oct 23
3672
1
Jun 22
4914