Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

How to use python functions in view(xml)?

By
vivek
on 4/30/13, 4:01 PM 4,635 views

Hi,

I am having a domain filter for object res.partner like the below in contacts_view.xml

    <field name="domain">[('department_id','=',context.get('dept'))]</field>

I would like to do the following

  • Get the current user object (my res_user object has id & dept)
  • i would get the id from context as it's stored by default by session. i like to get the dept from the context

I've tried this already

from openerp.osv import fields,osv
from openerp.tools.translate import _
from decimal import Context

class res_partner(osv.osv):
_name = "res.partner"
_inherit = 'res.partner'

def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
    res = super(res_partner,self).fields_view_get(cr, uid, view_id, view_type, context, toolbar=toolbar, submenu=submenu)
    if context is None:
        context={}

    context.update({
        'dept': 1
    })
    res.update({
        'context' : context,
    })
    return res     
res_partner()

Still in the filter i am unable to get the dept value from context.

How to get the department from the user object in xml? or how to use the python function in xml? This prob is really annoying me so much. Thanks a lot for your time.

0
klacus
On 5/1/13, 1:30 AM

Hi. I am not a python giant :-) So sorry for advance if this is not good, but I want to share with you...

idk = context.get('active_id',False) >> return idk as integer

idk = context.get('active_ids',False) >> return idk as list

in this case for id in idk: use id for something

so may be... :-) :

def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
    res = super(res_partner,self).fields_view_get(cr, uid, view_id, view_type, context, toolbar=toolbar, submenu=submenu)
    if context is None:
        context={}
    idk = context.get('active_ids',False)  # return idk as list
    for id in idk:
    #your object manipualtion according by id
0
nazarii
On 4/30/13, 5:06 PM

You should update the context before calling super so it will looks like:

def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
    if context is None:
        context={}

    context.update({
        'dept': 1
    })
    return super(res_partner,self).fields_view_get(cr, uid, view_id=view_id, view_type=view_type, context=context, toolbar=toolbar, submenu=submenu)

Hi nazeril, thanks for your reply. Still having the same problem. Can you guess wht went wrong?

vivek
on 4/30/13, 5:15 PM

You can always update domain from context by modifying search method of model. Besides, my name is Nazarii, not nazeril!!!

nazarii
on 5/1/13, 7:50 PM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

0 follower(s)

Stats

Asked: 4/30/13, 4:01 PM
Seen: 4635 times
Last updated: 3/16/15, 8:10 AM