Spirit angel
Spirit angel

Reputation: 191

OpenERP :Simple use of fields.function

I try to make some calculation of fields of my object and store them to a new field. I'm beginning with a simple example of using fileds.function, but when I try to login to openerp, the system raise an error that the user or password is incorrect.

in my class I add the field:

      'a' : fields.integer('A'),
      'b' : fields.integer('B'),
      'total' : fields.function(fnct, method=True, string='Tot',type='integer'),

definition of the function:

       def fnct(self, cr, uid, ids, fields, arg, context):

          x = {}

          for record in self.browse(cr, uid, ids):

              x[record.id] = record.a + record.b

          return x

Please ,can anyone help me ? thanks

Upvotes: 5

Views: 6359

Answers (3)

Abidh
Abidh

Reputation: 1

def fnct(self, cr, uid, ids, fields, arg, context):

    x = {}

    for record in self.browse(cr, uid, ids):

    x[record.id] = record.a - record.b

if x[record.id]<0:

    raise osv.except_osv(("Warning"),("You Cant Subtract %s ")%(record.a - record.b))

else:


return x

    "a":fields.integer('A'),
    "b":fields.integer('B'),


 "total":fields.function(fnct, method=True, string='Total',type='integer'),

Upvotes: 0

Spirit angel
Spirit angel

Reputation: 191

To fix that issue, I check for some intendation and also the definition of my function should be before declaration of fields .

Upvotes: 2

Avadhesh
Avadhesh

Reputation: 4703

There is no Connection of Function Filed with OpenERP Login.

So It may be possible that you are providing wrong user id or password.

The Main Use of Function Field is:

Auto Calculate Value of the field based on other Fields.

i.e Total = field1 + field2 + field3

Example: 'total' : fields.function(get_total, method=True, string='Total',type='integer'),

How to define Function:

def get_total(self, cr, uid, ids, fields, arg, context):

    x={}

    for record in self.browse(cr, uid, ids):

        x[record.id]= record.field1 + record.field2 + record.field3

    return x

Upvotes: 4

Related Questions