Question

I have a function field, but I don't know what should the function return.

Here's my code:

the function:

def _property_expense_preset_expenses(self, cr, uid, ids, expenses, arg, context):
    spus = self.browse(cr, uid, ids)
    _spu = False
    for spu in spus:
    _spu = spu

    if(_spu):
        expenses_acc = {}
        property_expense_presets = _spu.property_expense_presets
        for property_expense_preset in property_expense_presets:
            expenses = property_expense_preset.expense_preset.expenses
            for expense in expenses:
            expenses_acc[expense.id] = expense
        return expenses_acc
    else:
        return {}

The field definition:

'expenses'      : fields.function(
                _property_expense_preset_expenses,
                type='one2many',
                obj="property.expense",
                method=True,
                string='Expenses'
            ),

The code above doesn't work, it raises an error : KeyError: 788

Was it helpful?

Solution

Like all function fields, it must return a dictionary with an entry and value for every ID you get passed in ids, although your value can be False, None, []

In your case your functional field is declared as a one2many type which means your functional field must return a dictionary with an entry per id and the value, a list of integers that represent the ids of the related table, in your case, property.expense.

A very common pattern is:

def _property_expense_preset_expenses(self, cr, uid, ids, field, arg, context = None):
    res = {}
    for spu in self.browse(cr, uid, ids, context = context):
        res[spu.id] = []
        for preset in spu.property_expense_presets:
            res[spu.id].extend([x.id for x in preset.expense_preset.expenses])

    return res

Assuming ids contains 1,2,3 you will get a result of {1: [...], 2: [...], 3: []}

Where each list contains the integer ids of the expenses or an empty list if there are none.

As a general comment, I note your code doesn't default the context argument to None or pass the context as a named argument to the browse method - it is important to do both.

OTHER TIPS

What is Function Field? What return this functional field function?

In OpenERP functional field is a field that return the calculated/logical value that value store into table. That value you can not get directly. that's why we want to use function field and return some value.

When you inserting data into object model functional field call every time defined function and that function logic code whatever you made and return that functional field value.

Example

class me.branch(osv.osv):
    _name = "me.branch"
    _order = 'name'

    def _get_branch_name(self, cr, uid, ids, field_name, arg, context=None):
        r = {}
        for branch in self.browse(cr, uid, ids, context=context):
            r[branch.id] = branch.name.split('/')[-1]
        return r

    _columns = {
        'name': fields.char('Name', required=True),
        'branch_name': fields.function(_get_branch_name, type='char', string='Branch', readonly=1, store=True),
    }

Above example code branch name (eg. saas/demo/rarone ) already exist in another table.

But I want to get that branch name only last slash (/) after string (rarone) and store into this table only.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top