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

Concatenate two fields type float and char?

By
Tajjajt
on 2/27/13, 10:16 AM 4,212 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

0
Lucio
On 2/27/13, 1:34 PM

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

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!

Lucio
on 2/28/13, 7:26 AM

thanks Lucio ... problem solved

Tajjajt
on 2/28/13, 12:57 PM
0

Nicolas Bessi

--Nicolas Bessi--
1590
| 5 7 8
Lausanne, Switzerland
--Nicolas Bessi--
Nicolas Bessi
On 2/27/13, 11:16 AM

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

-1
MALLE SOULEYMANE
On 5/13/13, 7:45 PM

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
-1
Tajjajt
On 2/27/13, 12:02 PM

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

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

0 follower(s)

Stats

Asked: 2/27/13, 10:16 AM
Seen: 4212 times
Last updated: 3/16/15, 8:10 AM