Skip to Content
Menu
This question has been flagged
3 Replies
7807 Views

hi how to convert numbers into percent and display it in another field. let say I input a number in percent field. I input 82.10 then if i click save it will display in another field to 82.10%. can anyone help me? please i need help on it. please. i need a sample working code on this..

Avatar
Discard

so it is possible?? could you give me a sample code? the working one? please i really need your help

I just updated my answer

Author

what is the use of @api??

Author

when i use @api.one; theres an red x icon that says undefined variable: api

from openerp import models, fields, api im using the new api for v8 in my code official documentation:https://www.odoo.com/documentation/8.0/reference/orm.html

you can search in the documentation what is the @api.one for, and how to make calculated(function) fields.

Author

==================================================================================================now its working after that i need to multiply it on another field. but i cant multiply because the error said int to str ... blaaaa3x.... so any idea? gow to do it? because my main purpose is to multiply it to another field

Best Answer

Hi Louie,

First

I will try to correct your code also. You can try like this, I hope it works:


class appraisal_report(osv.Model):
    _name = 'appraisal.report'
    _description = 'Appraisal Report'
    
    def _show_percent(self, cr, uid, ids, field_names=None, arg=False, context=None):
        res = {}
        for rec in self.browse(cr, uid, ids, context=context):
            res[rec.id] = str(rec.numberpercentage) + "%"
        return res
    _columns = {
         'numberpercentage': fields.float('Number', required=True),
          'percentagedisplay': fields.function(_show_percent, string="Percentage", type='char')
        }

 

I can see that, your code is using old api style, but can work in odoo 8 and code by Felipe is fully using the new api.

In Felipe's code,  you can see '@api', they are called method decorators. You can refer the link given by Felipe to know it in detail, but I will just try to give an idea. The new api deals with recordset concept. And it also eliminates the need to pass cr, uid, context while calling a function, as they are implicit in new api. For example, you can call a function like this:
 
self.env['model.name'].func_A(). The function will be defined as

def func_A(self):

      #function body

Here self is the recordset. For dealing with that self or recordset, we use "@api.one". It avoids the need to iterate in the recordset.

For example, I have 3 fields A, B and total. Then if you used '@api.one', in function you can write like, self.total = self.A * self.B instead of,

for record in self:
   record.total = record.A * record.B

That means if you use @api.one, it will automatically iterate in the recordset, otherwise you need to use for loop.

And Felipe has used @api.depends('the_number'), it is used with compute fields (replaces the functional fields types) in new api. That means here we can say, field 'the_percantage' depends on the field 'the_number'. So whenever you change 'the_number' value, it will automatically change 'the_percentage' based on that, as on_change is implicit for compute fields in the new api.

So I hope you better understood the code of Felipe. His answer is right based on your question I believe. Also, on using his code you need to do the import at top of .py file like this: from openerp import models, fields, api

 

Hope this helps you...

Avatar
Discard
Author Best Answer

hi thanks on that. what you mean by 'the_number'? anyway here's my sample code and i dont where to insert the percentage code.

  class appraisal_report(osv.Model):                                                                                                                                                           _name = 'appraisal.report'                                                                                                                                                                     _description = 'Appraisal Report'                                                                                                                                                           _columns = {                                                                                                                                                                                                          'numberpercentage': fields.integer('Percentage', required=True),                                                                                                           'percentagedisplay': #here i do know what code and also for the code that you give also#                                                                 }                                                                                                                                                                       #but i will try to understand the code that you give to me. please i really need your help.. thanks so much....     

 

==================================================================================================now its working after that i need to multiply it on another field. but i cant multiply because the error said int to str ... blaaaa3x.... so any idea? gow to do it? because my main purpose is to multiply it to another field                                                                                                                                                                                                                                   

Avatar
Discard
Best Answer

just put a '%' in the label, or you can do a funcion that takes the float number change it to string and add it %(but this is kinda ridiculous)

---UPDATE---

class new_stuff(models.Model):
    _name = 'new.stuff'

    @api.one
    @api.depends('the_number')
    def _percentage_string(self):
        self.the_percentage=str(self.the_number)+'%'

    the_number=fields.Float()
    the_percentage=fields.String(compute='_percentage_string')

Avatar
Discard