nick
nick

Reputation: 51

openerp function field not working - OpenERP v7

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.

Upvotes: 2

Views: 1302

Answers (6)

Hussain
Hussain

Reputation: 1

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.

Upvotes: 0

shravan cherukuri
shravan cherukuri

Reputation: 490

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

Upvotes: 0

Atul Arvind
Atul Arvind

Reputation: 16743

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.

Upvotes: 1

Juan Salcedo
Juan Salcedo

Reputation: 1668

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!

Upvotes: 0

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

Upvotes: 0

DASADIYA CHAITANYA
DASADIYA CHAITANYA

Reputation: 2892

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 :)

Upvotes: 0

Related Questions