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 fix IndexError: list index out of range ?

By
Trai Doan
on 11/17/14, 10:51 AM 989 views

class name(osv.osv):
        _name='name.model'
        _columns={'name':fields.char('Name',size=32),
                  'so':fields.integer('So luong'),
                  'cost':fields.integer('Cost'),
                  }
name()

class second_model(osv.osv_memory):
        _name='second.model'
        _columns={
                  'name1':fields.many2one('name.model','Name1'),
                  'name2':fields.many2one('name.model','Name2'),
                  'select_cost':fields.integer('Select Cost')         
                  }
        def onchange_cost(self,cr,uid,ids,name1,context=None):
            value={}
            if name1:
                record_id=self.pool.get('name.model').search(cr,uid,[('name','=',name1)],context=context)
                record=self.pool.get('name.model').browse(cr,uid,record_id[0],context=context)
                value['select_cost']=record.cost
            else:
                value['select_cost']=0    
            return {'value':value}
second_model()

<record id="model2_form" model="ir.ui.view">
        <field name="name">teb</field>
        <field name="model">second.model</field> 
        <field name="type">form</field>
        <field name="arch" type="xml">
                <form>
                    <group col="2">
                         <field name="name1" on_change="onchange_cost(name1)"/>
                        <field name="name2"/>
                        <field name="select_cost"/>
                    </group>
                </form> 
         </field>
    </record>

When name1 is selected , this error appear 

File "C:\openerp-8.0rc1\openerp\addons\Baitap05\core.py", line 30, in onchange_cost record=self.pool.get('name.model').browse(cr,uid,record_id[0],context=context) IndexError: list index out of range

 I'm new in odoo ,please help me .Thanks

1

Ludo - Neobis

--Ludo - Neobis--
2387
| 2 2 5
Hoogvliet, Netherlands
--Ludo - Neobis--

Developer

Ludo - Neobis
On 11/17/14, 11:27 AM

The search you do on name.model is coming up with no results. You should check if record_id contains any records before perfoming a browse on it.

Thanks for replay .I added some code : if record_id: record=self.pool.get('tt_kho').browse(cr,uid,record_id[0],context=context) value['ten']=record.ten else: value['ten']='haha' It looks like it can't get record_id and value['ten'] always = 'haha' ,please help me solve this .Thanks

Trai Doan
on 11/18/14, 9:04 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

1 follower(s)

Stats

Asked: 11/17/14, 10:51 AM
Seen: 989 times
Last updated: 3/16/15, 8:10 AM