Skip to Content
Menú
This question has been flagged
1 Respondre
8895 Vistes

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
Descartar
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
Descartar
Autor

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.

Related Posts Respostes Vistes Activitat
12
de des. 17
24736
2
de març 15
7318
1
de març 15
4242
0
de des. 24
9471
3
de nov. 24
29959