1
votes

How can I get label names for fields in any model?

For example writing this:

obj = self.read(cr,uid,ids)[0]

It will get dictionary of fields names (technical names, or in database column names) and their values in a given record.

So for example output would be something like this:

{'field1': 10, 'field2': 'hello', 'field3': 3.56}

But it does not return labels of fields, and I can't access it from here too, because field names here is just a string.

So let say there would be these fields mentioned above:

_columns = {
    'field1': fields.integer('First Field'),
    'field2': fields.char('Second Field', size=128),
    'field3': fields.float('Third Field'),
}

Then how could I get this instead (I know exact output is not possible, because label is not the key, but I'm just showing it to better understand the problem):

{'First Field': 10, 'Second Field': 'hello', 'Third Field': 3.56}

So I think code for retrieving labels could look something like that:

for k, v in obj.iteritems():
    print k.label

But there is no such attribute. In fields.py file I saw in most field types string named attribute is used as input for labels, which has default input string='unknown', but I don't get it how to retrieve it while iterating over all fields in a model.

Anyone know how to do it?

P.S. Why I need this? I need to find fields whose values meet specific condition and then write that field label in other table. I could write column name, but simple users will need to see that data, so they need to understand what that field means,, thats why I need to retrieve field labels.

3

3 Answers

6
votes

there are two tables you can use ir.model and ir.model.fields. on the second one you will get the "labels" by using the field "name".

so search for fields with a specific model get there labels. :)

for further questions just ask :)

edit: an example could be sale.order, if i want all labels for m2o relation fields with res.partner

...
model_obj = self.pool.get('ir.model')
imf_obj = self.pool.get('ir.model.fields')
field_label_list = []
model_id = model_obj.search(cr, uid, [('model','=','sale.order')], context=context)
if model_id:
    field_ids = imf_obj.search(cr, uid, [('model_id','=',model_id[0]),('ttype','=','many2one'),('relation','=','res.partner')], context=context)
    if field_ids:
        for field in imf_obj.browse(cr, uid, field_ids, context):
            field_label_list.append(field.field_description)
#do what you want with the list
...

second edit: at class attribute _columns you will have all fields. your desired attribute for them is string

for field in self._columns.itervalues():
    print field.string
2
votes

Hi i think in odoo version 11 its a more easier task to accomplish.

Just use

._fields()

and you will get the field labels.

You can find more useful function by using dir() in odoo console.

Hope that will help.

Thanks.

0
votes

Do this:

self.fields_get('fieldname')

Example:

self.fields_get('phonenumber')

Output will be:

{'phonenumber': {'change_default': False, 'string': u'Phone', 'searchable': True, 'required': False, 'manual': False, 'readonly': False, 'depends': (), 'company_dependent': False, 'sortable': True, 'translate': False, 'type': 'Float', 'store': True}}