This question has been flagged
1 Reply
7225 Views

I have a model and I want to automatically fill one field of this model.

E.g. I have some test model and I want to automaticly genrate EAN13 for this after adding new product:

class product_test(osv.osv):
    _name = "product.test"
    _description = "Product test"
    _table = "product_test"
    _columns = {
        'name': fields.char('name', size=64, required=True, translate=True, select=True),
        'ean13': fields.char('EAN13', size=64, readonly=True),
    }
    _order = "name"
Avatar
Discard
Best Answer

Hi,

must be overwrite method create

def create(self, cr, user, vals, context=None):           
        new_id = super(product_test, self).create(cr, user, vals, context)
        //your treatment
        return new_id

or use _defaults {}

  _columns = {  'ean13' : fields.char('ean13', size=64, ), }

  _defaults = {   'ean13': _get_ean13, }

  def _get_ref(self, cr, uid,context, *args):
        ean13 = // your treatment
        return ean13

Thanks.

Avatar
Discard
Author

Thank you! But when I install my addon with _defaults it writes the same EAN13 to all existing fileds.

in _defaults you make a call to a function _get_ean13 which treatment you want it can return a new ean13 for each new product and not same ean13.