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.

1

name get() not working ?

By
omprakash
on 6/6/13, 1:17 PM 5,844 views
Hai Friends ,
    First I thank everyone for such good support for newbies . I have Question regards name_get() . This is my sample custom code .Please let me know how to pass value for context (for many2one field i like to display to email instead of title) .

Notebook :
1. __init__.py
   import notebook
2.__openerp__.py
{
    "name" : "notebook",
    "version" : "0.1",
    "author" : "Prakash",
    "website" : "http://www.prakash.com/",
    "category" : "Generic Modules/Others",
    "depends" : ["base"],
    "description" : "Simple demo module",
    "init_xml" : ['notebook_view.xml'],
    "demo_xml" : [],
    "update_xml" : [],
    "active": False,
    "installable": False
}
3.notebook.py
 from osv import fields, osv
 class notebook(osv.osv):
    _name = "notebook"
    _description = "Simple Notebook"
    _rec_name = 'title'
    _columns = {
        'title' : fields.char('Title', size=30, required=True),
        'note' : fields.text('Note'),
        'note_date' : fields.date('Date'),
        'email': fields.char('Email', size=240),
    }
    def name_get(self, cr, uid, ids, context=None):
        if context is None:
            context = {}
        res = []
        for record in self.browse(cr, uid, ids, context=context):
            email = record.email
            title = record.title
            if context.get('email'):
                email = "%s <%s>" % (email, record.email)
                return res
            res.append((record.id, title))
        return res

notebook()

 4. notebook_view.xml
    <?xml version="1.0" encoding="utf-8"?>
<openerp>
    <data>
        <record model="ir.ui.view" id="notebook_tree_view">
            <field name="name">notebook.tree</field>
            <field name="model">notebook</field>
            <field name="type">tree</field>
            <field name="arch" type="xml">
                <tree string="Notebook">
                    <field name="title"/>
                    <field name="note"/>
                    <field name="note_date"/>
                </tree>
            </field>
        </record>
        <record model="ir.ui.view" id="notebook_form_view">
            <field name="name">notebook.form</field>
            <field name="model">notebook</field>
            <field name="type">form</field>
            <field name="arch" type="xml">
                <form string="Notebook">
                    <field name="title"/>
                    <field name="note"/>
                    <field name="note_date"/>
                    <field name="email"/>
                </form>
            </field>
        </record>
        <record model="ir.actions.act_window" id="action_notebook_form">
            <field name="name">notebook</field>
            <field name="res_model">notebook</field>
        </record>
        <menuitem name="NotebookParent" icon="terp-project" id="NotebookParent_menu"/>
    <menuitem name="NotesChild" parent="NotebookParent_menu" id="NotesChild_menu" />
        <menuitem name="Header" parent="NotesChild_menu" id="Header_menu_mainform" action="action_notebook_form"/>
    </data>
</openerp>



monitor:

1. __init__.py
    import monitor
2. __openerp__.py
{
    "name": "monitor",
    "version": "7.0",
    "depends": ["base"],
    "author": "author",
    "category": "category",
    "description": """
    This module provide : test
    """,
    "init_xml": [],
    'update_xml': [],
    'demo_xml': [],
    'installable': True,
    'active': False,
    "data" : ["monitor_view.xml"]
}
3. monitor.py

  from osv import fields,osv

class monitor(osv.osv):
    _name = 'monitor'
    _columns = {
            'name' : fields.char("name" , size = 10 , required = True),
            'email':fields.many2one('notebook', 'email'),
                    }
monitor()

4.monitor_view.xml
 <?xml version="1.0" encoding="UTF-8"?>
<openerp>
    <data>
               <record model="ir.ui.view" id="monitor_form_view">
                <field name="name">monitor.form</field>
                <field name="model">monitor</field>
                <field name="type">form</field>
                <field name="arch" type="xml">
                    <form string="form_monitor">
                        <field name="name" select="1"/>
                         <field name="email" context="{'email':email}"/>
                    </form>
                </field>
            </record>    

        <act_window name="monitor"
            res_model="monitor"
            src_model="monitor"
            id="monitor_id"/>
    <menuitem id="one_id" name="one" />
    <menuitem id="two_id" name="two" parent="one_id" />
    <menuitem id="three_id" name="three" parent="two_id" action="monitor_id"/>
    </data>
</openerp>

Actually I like to display email instead of title (many2one) , but still it getting title as display for many2one . I don't know how to pass value for context .Please help me to move further . And once again i thank everyone .
9

Sandeep

--Sandeep--
6083
| 6 5 8
Delhi, India
--Sandeep--
Python OpenERP Developer http://in.linkedin.com/pub/sandeep-rana/3b/a16/786/
Sandeep
On 6/7/13, 4:40 AM

your method should be

   def name_get(self, cr, uid, ids, context=None):
    if context is None:
        context = {}
    res = []
    for record in self.browse(cr, uid, ids, context=context):
        email = record.email
        title = record.title
        tit = "%s <%s>" % (title, record.email)
        res.append((record.id, tit))
    return res

Hi Sandeep , Thanks for your reply . I have doubt in < monitor_view.xml> line -> <field name="email" context="{'email':email}"/> how to pass value for def name_get(self, cr, uid, ids, context=None) . Actually I need how to pass context value ( need correct syntax) ...And once again thanks

omprakash
on 6/7/13, 9:08 AM

i think you no need to pass value for context for name get, just include above method and don't use context , as i think you want name get with email so email is already there

Sandeep
on 6/7/13, 9:17 AM

Sandeep Thanks for reply . Basically if i use relation (many2one ) title will be displayed because i declared _rec_name = 'title' (notebook). But i need to display email for some module (monitor). So i want to pass context which decide what to display . But i don't know the syntax . If you understand my problem please help me .

omprakash
on 6/7/13, 9:23 AM

for some records email and some for only title ??

Sandeep
on 6/7/13, 9:37 AM

Sandeep , For module < monitor > i like to display - email & if i have another module example <school> i want to display as title .

omprakash
on 6/7/13, 10:57 AM

@Sandeep. If I have large amount of data than it is possible to name_get method working? Its give memory error if I have more than 50000 records.

Solanki Shamji
on 12/1/14, 12:23 AM
0
Muhammad
On 5/29/15, 6:27 AM

Hi,

Can I use this name_get() function as well?

I am using the Automated Actions module to create a project automatically when an opportunity is created.

The only problem I have is that I don't know how to name the project based on the name of the lead/opportunity automatically.

Can you tell me what function/python code I should use?

-1
Alexander
On 6/6/13, 7:08 PM

Hi! It is too difficult to read unformated source code :)

Perhaps, using related email field instead of many2one field in view will be helpfull for you. Also it will be work faster, because no request will be send for every row.

Hi Alexander , Thanks for immediate reply . Now I formatted my code . Can you please help me

omprakash
on 6/7/13, 3:13 AM

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

4 follower(s)

Stats

Asked: 6/6/13, 1:17 PM
Seen: 5844 times
Last updated: 3/18/16, 10:31 PM