Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

numbers into percent

By
louie
on 2/15/15, 8:18 PM 882 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..

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

louie
on 2/16/15, 3:09 AM

I just updated my answer

Felipe
on 2/16/15, 9:56 AM

what is the use of @api??

louie
on 2/16/15, 9:34 PM

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

louie
on 2/16/15, 9:49 PM

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

Felipe
on 2/16/15, 10:05 PM

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

Felipe
on 2/16/15, 10:08 PM

==================================================================================================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

louie
on 2/19/15, 9:36 PM
2

Akhil P Sivan

--Akhil P Sivan--
3423
| 5 3 6
Kochi, India
--Akhil P Sivan--

Interested in exploring Odoo both technically & functionality wise.

email-id : akhilpsivan01@gmail.com

Akhil P Sivan
On 2/17/15, 2:17 AM

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...

1
louie
On 2/16/15, 9:31 PM

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                                                                                                                                                                                                                                   

1

Felipe

--Felipe--
962
| 5 3 8
Bogotá, Colombia
--Felipe--
Informatics engineer Industrial engineer
Felipe
On 2/15/15, 8:32 PM

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')

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 2/15/15, 8:18 PM
Seen: 882 times
Last updated: 3/28/15, 3:13 AM