2
votes

I am having issues with getting a function field to display a string returned by a function in a custom module I am using to extend the stock.picking.out form. Ideally I would like to have a specific string from a field stored in the database displayed but that's an issue for another time because I can't even get a generic string to display..

Here is my field definition within my custom class:

_columns = {
    'my_field_name': fields.function(_my_func, type='char', string='description', store=True, method=True, readonly=True),
}

Here is my definition for '_my_func':

def _my_func(self, cr, uid, ids, field_name, arg, context=None):
    str="some string to be displayed"
    return str

Here is the XML for the field:

<field name="my_field_name" string="Here is my string:" class="oe_inline"/>

I have searched the OpenERP dev book as well as their forums and these forums, and believe I have followed all of the proper syntax for this field so any help is greatly appreciated.

6

6 Answers

1
votes

You need to improve your function/method like,

def _my_func(self, cr, uid, ids, field_name, arg, context=None):
    res = {}
    for rec in self.browse(cr, uid, ids, context=context):
        res[rec.id] = 'Some string'
    return res

while writing functional field(up to v7) make sure you return a dictionary like

{17: "some string"}

if you have multiple field it can be like

{17: {'field_one': 'value 1', 'field_two': 'value 1'}}

EDIT: Also In _columns please remove readonly=True like

_columns = {
    'my_field_name': fields.function(_my_func, type='char', string='description', store=True, method=True),
}

Hope this helps.

0
votes

Just you must have to improve your function login

you can do some things like this.

def _my_func(self, cr, uid, ids, field_name, arg, context=None)
    v={}
    if field_name:
        v['my_field_name'] = " some string to be displayed "
    return {'value': v}

Then finally your function logic working like a charm.

I hope this should helpful for you :)

0
votes

Try following,

def _my_func(self, cr, uid, ids, field_name, arg, context=None):
    res = {}
    for obj in self.browse(cr, uid, ids, context=context):
        ###Set your string here
        str="some string to be displayed"
        res[obj.id]['my_field_name'] =  str

    return res
0
votes

If you are a little confused with OpenERP v7, you always can try with Odoo(OpenERP v8) It's much more easiest to work.

from openerp import models, fields,api

class my_class(models.Model):

_name="my.class"
my_field_name=fields.Text(string="description", compute=_my_func, store=True, readonly=True)

def _my_func(self):
    self.my_field_name = "some string to be displayed"

I hope this can help you!

0
votes

try this,

def _my_func(self, cr, uid, ids, field_name, arg, context=None):
    result = {}
    for record in self.browse(cr, uid, ids):
        str="some string to be displayed"
        result[record.id] = str
    return result

and if you use store = True on a functional-field then it will be accessed for the first time and it will store the result in database, and from next time it wont be called. To access functional field every time for store = True , refer sale.py [amount_total] field. We have to write some addtional code

0
votes

You can give like this also :

without using browse method..

def _my_func(self, cr, uid, ids, field_name, arg, context=None):
    result = {}
    str="some string to be displayed"
    result[ids[0]] = str
    return result

Make sure, in model field type should be char/text.