Community mailing list archives

community@mail.odoo.com

Re: best way to generate a field

by
Ludwik Trammer
- 01/27/2015 06:36:36

And there is also another problem - since you use randomness the code will change every time the field is recomputed. For non stored files that means all the time. I assume that's not what you want (?). So looking at the specifics maybe using computed field is not such a good idea in this case...


Ludwik



On Tue Jan 27 2015 at 12:23:38 PM Ludwik Trammer <ludwik@gmail.com> wrote:
The second one looks almost correct, but I don't know where did the "related_vals" came from. The line should read:

    self.ean13 = barcode.get_barcode('ean13', code).get_fullcode()

"_compute_default_code" looks very incomplete. Also, you use the old API (style) for defining model fields. 

Ludwik

On Tue Jan 27 2015 at 12:12:48 PM Fabrizio Mancini <mr.file@gmail.com> wrote:
Hi Ludwik,
thanks for the response.
Let me see if i have understood:
#overwrite default_code of product_product
'default_code' : fields.char(compute='_compute_default_code')
'ean13': fields.char('EAN13 Barcode', size=13, help="International Article Number used for product identification.", compute='_compute_ean13')

def _compute_default_code(self):
    self.pool.get('ir.sequence').get(cr, uid, 'product.product')

@api.depends('default_code')
def _compute_ean13(self):
    code = unicode(randint(0,99999)).rjust(5,'0') + unicode(self.default_code).rjust(7, '0')
    related_vals['ean13'] = barcode.get_barcode('ean13', code).get_fullcode()

Is this correct for you?
Thanks in advacne

2015-01-27 11:48 GMT+01:00 Ludwik Trammer <ludwik@gmail.com>:

Looks like a job for computed fields (provided you don't intend to be able to later edit them by hand independently of the fields they were generated from).


Computed fields documentation:

L.

On Tue Jan 27 2015 at 10:52:06 AM Fabrizio Mancini <mr.file@gmail.com> wrote:
Hi everyone,
i need to generate automatically two fields (ean13 and default_code) on the product_product model.
The default code is generated by a sequence, and the ean13 is generated from a random number plus the default code. All is conditioned by the fact that the suppliers_ids field should be present.
I'm using odoo8.0.
I would be glad to hear your suggestion to know what is the best way to do this.
Thanks in advance

_______________________________________________
Mailing-List: https://www.odoo.com/groups/community-59
Post to: mailto:community@mail.odoo.com
Unsubscribe: https://www.odoo.com/groups?unsubscribe

_______________________________________________
Mailing-List: https://www.odoo.com/groups/community-59
Post to: mailto:community@mail.odoo.com
Unsubscribe: https://www.odoo.com/groups?unsubscribe


_______________________________________________
Mailing-List: https://www.odoo.com/groups/community-59
Post to: mailto:community@mail.odoo.com
Unsubscribe: https://www.odoo.com/groups?unsubscribe