Share variable between functions in models

    class VendorChicken(ModelSQL, ModelView):
        "Vendor Chicken"
        
        __name__ = 'vendor.chicken'
        chicken_number = fields.Char('Number')
        barcode_chicken = fields.Binary('Barcode', required=True )
        chicken_code = fields.Function(fields.Char('Chicken Code',readonly=True),'get_chicken_code')

        
        @classmethod
        def default_barcode_chicken(self):
            global nums
            fd = BytesIO()
            image = barcode.generate('ean13', nums ,writer=ImageWriter(), output=fd)
            fd.seek(0)
            return fields.Binary.cast(fd.read())
        
       def get_chicken_code(self,name):
           return self.chicken_number

       @classmethod
       def default_chicken_number(self):
             global nums 
             return nums

how can i share the same random number between the different function in model i tried implementing global var but does not seems to work

i had to assign the same ean number of barcode to another field in form

is there any way i can assign the ean number directly to char field

I do not see the point to store the barcode if it is just generated from the chicken_number. So it could be just a Function field that use the value of chicken_number. Of course you can not have a default method for it but using a on_change_with_barcode_chicken as getter will update it on the fly.
Also what is the point to duplicate the chicken_number into the chicken_code. It is simpler to have a unique source of data.

    class VendorChicken(ModelSQL, ModelView):

        "Vendor Chicken"

        

        __name__ = 'vendor.chicken'
        
        chicken_code = fields.Function(fields.Char('Chicken Code'),'get_chicken_code') 
        barcode_chicken = fields.Function(fields.Binary('Barcode'),'on_change_with_barcode_chicken') 
     
       @fields.depends('chicken_code')
        def on_change_with_barcode_chicken(self , name=None):
              fd = BytesIO()
              image = barcode.generate('ean13', self.chicken_code ,writer=ImageWriter(), output=fd)
              fd.seek(0)
              return fields.Binary.cast(fd.read())

    
       def get_chicken_code(self,name):
             c = str(random.randint(100000000000,999999999999))
             return c  
        

i just give that a try but turns out the chicken_code changes every time when i open the forms

Of course you make it random.
You must store the value if you want to keep it and not use a Function field which behaves randomly.

yes i mean it changes even editing the forms that changes the barcode every time

Yes you made the value of chicken_code random each time it is read.

Do i have to input the number manually or is there any option to generate random 12 digit number which will not change every time on opening the form ?

You have to store for each record the random number generated.
So you could define a default value for the field but in this case you must disable the cache on the RPC of default_get (otherwise the same value will be used for all record created by the same client).
Or you can extend the ModelStorage.create method to fill the field with random value (like it is done for ir.session).

@classmethod
def __setup__(cls):
    super().__setup__()
    cls.__rpc__['default_get'].cache = None

this sorted my issue out thanks cedric . by the way i can’t figure out the use of cache

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.