This question has been flagged
3 Replies
3112 Views

I want to display dynamically a list of values on a field, so I choosed a functionnal field, type = selection, like this : 

'Numeros_bl': fields.function(_get_selection, string ='Numeros Bls', type = 'selection'),

The function is like this : 

def _get_selection(self, cr, uid, ids, Numeros_bl, arg, context = None):

        default = {
            'value': {'Numeros_bl': {} }
        }
        DO = self.pool.get('stock.picking.out')
        rec = self.browse(cr, uid, ids[0])
        partner_id = rec.partner_id
        do2pay_ids = DO.search(cr, uid, [('partner_id','=',partner_id),('state','=','done'),('invoice_state','=','2binvoiced')])
        for do2pay in do2pay_ids :
            do_rec= DO.browse(cr, uid, do2pay)
            rs = {
                'Numeros_bl': do_rec.num_bl,
            }
            default = default.append(rs)
        return default

The problem is that this function return nothing, and I don't have any server errors, so maybe it return an empty return.

I know there is something wrong maybe with :   default = {
            'value': {'Numeros_bl': {} }
        }

or :             rs = {
                'Numeros_bl': do_rec.num_bl,
            }
            default = default.append(rs)

or the return type, because the the function should return a selection type, because I made type = selection on the decalaration of the field, can you please tell me where I did it wrong ?

Avatar
Discard
Best Answer

Hi,

According to the following ex. you can make the dynamic selection values.

Field as like below.

'extension': fields.selection(_extension_get, 'Document Type', required=True, size=4),

Function is as like below.

    def _extension_get(self, cr, uid, context=None):
        cr.execute('select code,name from document_directory_content_type where active')
        res = cr.fetchall()
        return res

Note : This is available on the document module.

 

Avatar
Discard
Author Best Answer

Hi,

Thank you for your anwser, I did exactly the same thing, here is my code : 

from openerp.osv import fields, orm, osv

class AccountVoucher(orm.Model):

    def _get_selection(self, cr, uid, context):

        active_id = context.get('active',[])
        voucher_rec = self.browse(cr, uid, active_id)
        partner = voucher_rec.partner_id.id
        cr.execute("SELECT num_bl,num_bl FROM stock_picking WHERE state='done' and invoice_state='2binvoiced' and partner_id=%s",(partner,))

        return cr.fetchall()

    _inherit = 'account.voucher'
    _name = 'account.voucher'
    _columns = {
        'bls_to_pay': fields.one2many('stock.picking.out','bl_id','BLS to pay', readonly = True),
        'Numeros_bl': fields.selection(_get_selection, 'Numeros Bls', size=32),
    }

I want to pass the partner_id into the function _get_selection for that I think there is two solutions ( wich I wish to be corrected if wrong )  :

Passing directly the partner_id on the context via XML.

Passing the id of the current recored via ids[0] or active_id and browse on the current record id, then get the field partner_id.

I used selection field ( not functionnal field ) it doesn't take ids as a parameter, and I tried to pass the partner_id by XML context like this :  <field name="Numeros_bl" context="{'active': active_id}"/>

Nothing of these solutions is working.

The problem now is how to get the partner_id so as to use it on the where condition ? Should I use a functionnal field instead of dynamic selection ? 

Avatar
Discard
Best Answer

Hello yassine TEIMI,

I don't understand want you want to do, you wish to display a list of values coming for a one2many field ?

Do you want the user be able to modify this value ?

If yes for the 2 questions, you have another way to do that (as the _get_selection() method is called  before having the current record on context, this is not the correct way to do that): 

You can define your field Numeros_bl as a many2one (related to stock.picking.out) instead of a selection, and then in your view, you use the widget "selection" to display each element of this model as a selection field.

Finally, to filter your model with partner_id, you can do that too in the view adding the correct domain, which results in : 

<field name="Numeros_bl" widget=selection" domain="[('partner_id.id', '=', partner_id)]" />

 

This will work if your model stock.picking.out has a "name" field, or at least a "nam_get()" method defined, it uses those values to display each record on a many2one / selection widget.

Does it helps you ?

Regards

Avatar
Discard