コンテンツへスキップ
メニュー
この質問にフラグが付けられました
10 返信
18708 ビュー
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 .
アバター
破棄
最善の回答

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

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 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 .

for some records email and some for only title ??

著作者

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

@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.

最善の回答

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?

アバター
破棄
最善の回答

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

関連投稿 返信 ビュー 活動
0
3月 15
3932
1
6月 15
4832
List view 解決済
1
2月 25
1128
1
2月 25
19424
2
6月 23
15511