Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
3 Trả lời
16164 Lượt xem

As describe in https://doc.odoo.com/6.0/developer/2_5_Objects_Fields_Methods/field_type/#relational-types

I have created a many2one field with custom selection function :

def _get_partner_sup(self, cr, uid, context=None):
    obj = self.pool.get('res.partner')
    ids = obj.search(cr, uid, [('supplier','=',True), ('is_company','=',True)])
    res = obj.read(cr, uid, ids, ['name', 'id','email'], context)
    res = [(r['id'], r['name']) for r in res]    
    return res

_columns = {
    'partner_sup_id': fields.many2one('res.partner', 'Select Supplier 2 ', selection=_get_partner_sup),
}

But it's still like a normal many2one field. What is wrong ?

And I don't want use widget='selection' or domain=[('supplier','=',True), ('is_company','=',True)]

Thanks in Advance

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

Wrong parameter. The many2one field doesn't have parameter "selection". 

Can you explain please, why you don't want to use:

_columns = {
'partner_sup_id': fields.many2one('res.partner', 'Select Supplier 2 ', domain=[('supplier','=',True), ('is_company','=',True)),
}
you can also set domain in the XML, if you like to have domain filter only in the particular page.

?

if you want to limit displayed columns(fields) to the 'name' field in the selection(you seem to try this in your function), then you've to achieve it with custom view (treeview) for this model.

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

You can use any of the provided Options here but the final adjustment is that you need to set the widget selection for the field in your xml view definition. For example:

#in your .py  

_columns = {

    'partner_sup_id': fields.many2one('res.partner', 'Select Supplier 2 ', domain=[('supplier','=',True), ('is_company','=',True)),

}

 

#in your xml
 

<field name="partner_sup_id" widget="selection">
 

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất


Well if you don't want to use Domain / Widget...

Then you can try with either of these options

Option 1:

Define a field as Selection, instead of Many2One, along with a selection to achieve your requirement...

Coming to your Requirement:

def _get_partner_sup(self, cr, uid, context=None):

obj = self.pool.get('res.partner')

ids = obj.search(cr, uid, [('supplier','=',True), ('is_company','=',True)])

res = obj.read(cr, uid, ids, ['name', 'id','email'], context)

res = [(r['id'], r['name']) for r in res]

return res

_columns = {

'partner_sup_id': fields.Selection(selection=_get_partner_sup),

}

Option2: If your intention is just not to provide Create/Edit option to the users, then in XML, against that fields just "options={'no_create':True}", by doing this also, you can achieve the same...

Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
0
thg 10 20
3195
1
thg 8 17
3678
0
thg 3 15
3042
1
thg 3 15
6168
1
thg 9 23
1681