This question has been flagged
4 Replies
17667 Views

Hi!

I want to cancatenate two value of two fields one is float and the other is char, here my code :

class my_class(osv.osv):

    def _get_concatenate_values(self, cr, uid, ids, field_name, arg, context=None):
            records=self.browse(cr,uid,ids)
            result={}
            for r in records:
                if(r.field1 and r.field2):
                    result[r.id]=r.field1.name + " " + r.field2.name
            return result

    _name = 'my_class'
    _columns = {
      'field1': fields.many2one('class1', 'My string', required=True),
      'field2': fields.many2one('class1', 'My string', required=True),
      'code': fields.function(_get_concatenate_values, method=True, string='Reference', type='many2one'),
}

my_class()

I have this error :

in _get_concatenate_values TypeError: coercing to Unicode: need string or buffer, float found

PS: field1 ==> char field2==>float , it works when field2 is char

Avatar
Discard
Best Answer

Taking into account your response (you probably should not post as an answer), I think your problem might be in the definition of _get_concatenate_values

def _get_concatenate_values(self, cr, uid, ids, field_name, arg, context=None):
        records=self.browse(cr,uid,ids)
        result = dict((x,'') for x in ids)
        for r in records:
            if(r.field1 and r.field2)
                result[r.id] = "%s %f" % \
                         (r.field1.field1 or '', r.field2.field2 or 0.0)
        return result

and also, as nbessi posted, your functional field is not a of type many2one but char

Avatar
Discard

This part "%s %f" % (field1.field1 or '', r.field2.field2 or 0.0) Is like saying: I want a string which format is the one given. It is made of a string (%s) a blank space and a floating number (%f), which you provide after the % (%r.field1.field1 or '', r.field2.field2 of 0.0).

So to add '.', just change "%s %f" to "%s.%f" Hope it solves your problem!

Author

thanks Lucio ... problem solved

Hi,

What if I want to add a Slace(/) between two fields.

I tried but it gives error.

Best Answer

Hello,

Your code will not work.

'field1': fields.many2one('class1', 'My string', required=True),
'field2': fields.many2one('class1', 'My string', required=True),

You probabley mean:

 'field1': fields.char('My string', required=True),
 'field2': fields.float('My float', required=True),

Your function field is also incorrect:

'code': fields.function(_get_concatenate_values, method=True, string='Reference', type='many2one')

type should be char not many2one

You can't concatenate bool and string that mean if one of your fields is empty you will try to do str + False

You can do something like:

return "%s %f" % (r.field1 or '', r.field2 or 0.0)

If you want to set a rouding level you can use the: %0.2f to round to 2 digits

Avatar
Discard
Author Best Answer

Thanks for ur reply.

in fact the two fields are in an other class :

class class1(osv.osv):
    _name = 'class1'
    _columns = {
      'field1': fields.float('My float', digits=(2,0), required=True),
      'field2': fields.char('My string', size=64, required=True),
}

but i didn't get your remarque :

return "%s %f" % (r.field1 or '', r.field2 or 0.0) If you want to set a rouding level you can use the: %0.2f to round to 2 digits

Avatar
Discard
Best Answer

Hi. I have a problem, I would like to concatenate two char in one field, I do this code ut it does'nt work. Can you help me please.

def _my_methode(self, cr, uid, ids, field_name, arg, context=None):
    records=self.browse(cr,uid,ids)
    result = dict((x,'') for x in ids)
    for r in records:
        if(r.firstname and r.lastname)
            result[r.id] = "%s %s" % \
                     (r.firstname or '', r.lastname or '')
    return result
Avatar
Discard