It is currently Wed May 23, 2012 2:51 pm

All times are UTC + 2 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: display values like 0,53 in a field in form view
PostPosted: Thu Jul 28, 2011 1:22 am 
Offline

Joined: Tue Jan 18, 2011 5:16 pm
Posts: 236
I am making a module that shows area in square meters out of millimeters values,

I have a field that stores the area defined like this in the *.py file:
Code:
        'area_mp': fields.float(string='Area in m^2',  help="This is te area expressed in square meters.")         

in the xml file I have this:
Code:
                                <field digits="(14, 3)" name="area_mp" attrs="{'readonly':[('type','=','service')]}"/>


But the problem is that the area_mp field will only show integer values. I need to show also values below 0. Eg. 0,56.


And second problem is that I have
on change functions for width and length that changes the value of area_mp

Code:
    def onchange_length(self, cr, uid, ids, length, width, area_mp):
        if length:
            return {'value': {'area_mp': width*length/1000000}} #ver 1
             self.write(cr, uid, ids, {'area_mp': width*length/1000000}) #ver2
        return False


I want to use only one of them
ver1 works
ver 2 used to work but not anymore gives something about write concurrency. what could be wrong?
is there any other nice way to refresh a value in the form view?

Any help would be appreciated.

Thanks

_________________
OpenERP debugging with Eclipse on headless server


Top
 Profile  
 
 Post subject: Re: display values like 0,53 in a field in form view
PostPosted: Thu Jul 28, 2011 5:36 am 
Offline

Joined: Wed Jan 27, 2010 6:21 pm
Posts: 797
Location: Auckland, NZ
Hi,

Try defining digits in the field definition in the python module, and not the view. Although maybe you have reasons not to do this?

In regards to onchange.
General best practice is not to write to the object you are currently editing and a likely cause of the error, because if the user abandons their changes the value is already written. So ver 2 is not a good idea. ver 1 is better, and the write occurs on save.
Also it looks like you have 2 onchange functions, you really only need one.

But in general maybe these things are unnecessary. Why is area a type float? If it is always going to be a function of width and length, (as subtotal is a function of price and qty) then realistically it should be a function field of type float. Then it is always calculated for you although not until after save.

_________________
Graeme


Top
 Profile  
 
 Post subject: Re: display values like 0,53 in a field in form view
PostPosted: Thu Jul 28, 2011 10:40 pm 
Offline

Joined: Tue Jan 18, 2011 5:16 pm
Posts: 236
Hi,
as always your advice is very good.

I did defined the digits at python level, only one onchange function.

Still I am left with the initial problems, cannot show fractions, numbers less than 0. Eg : 0,53

Any clue? Anybody?

Thanks

_________________
OpenERP debugging with Eclipse on headless server


Top
 Profile  
 
 Post subject: Re: display values like 0,53 in a field in form view
PostPosted: Thu Jul 28, 2011 11:08 pm 
Offline

Joined: Wed Jan 27, 2010 6:21 pm
Posts: 797
Location: Auckland, NZ
try this

return {'value': {'area_mp': (width*length)/1000000.0}

The issue looks like you are dividing by an int.

_________________
Graeme


Top
 Profile  
 
 Post subject: Re: display values like 0,53 in a field in form view
PostPosted: Fri Jul 29, 2011 8:07 am 
Offline

Joined: Tue Jan 18, 2011 5:16 pm
Posts: 236
@gdaddy

Thanks, only after I've changed length and width to be floats the result was correct, before when they where integers the result was integer.
For me seemed natural that if the area_mp was a float, the result to be float.

also a second way was to keep the width and length as integer but then
Code:
           return {'value': {'area_mp': (float(width)*float(length)/1000000)}}


Thanks

_________________
OpenERP debugging with Eclipse on headless server


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC + 2 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:

Protected by Anti-Spam ACP