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

Function Returns Empty list Why? need help....... and explanation [Closed]

By
Odoo
on 7/22/14, 6:56 AM 420 views

The Question has been closed for reason: duplicate question

by
Odoo
on 08/08/2014 05:00:59

def button_dummy(self,cr,uid,ids,context=None):
        product_avail_obj=self.pool.get('bot.info').read(cr,uid,ids,['state'],context=context)
        val=product_avail_obj[0]['state']
        print "#################",val
        
        if val == 'not available':
            raise osv.except_osv(_('Hi!'), _('Product is Not Available Please Wait'))   
        else:
            return True

class bot_info(osv.osv):
    
    _name='bot.info'
    _description='Renatl info'
    
    _columns={
              'state':fields.selection([('draft','draft'),('available','available'),('not available','not available')],'state'),
              'id':fields.integer('ID'),
              'customer_name':fields.many2one('res.partner','Customer',required=True),
              'product_name':fields.many2one('product.product','Name',required=True),
              'quantity':fields.integer('Quantity',required=True),
              'issue_date':fields.datetime('Start Date'),
              'end_date':fields.datetime('End Date'),
              
              
              }
    
    def button_available(self,cr,uid,ids,context=None):
        self.write(cr,uid,ids,{'state':'available'})
        return True
    def button_notavailable(self,cr,uid,ids,context=None):
        self.write(cr,uid,ids,{'state':'not available'})
        return True
    
    def button_reset(self,cr,uid,ids,context=None):
        self.write(cr,uid,ids,{'state':'draft'})
        return True

 

#####ERROR

val=product_avail_obj[0]['state']

IndexError: list index out of range

 

0
klacus
On 7/22/14, 8:02 AM

Which function?

 

Ok.

Why not use a search function. (bit "standard")

like

            product_avail_obj=self.pool.get('bot.info')
            
            prod_id = product_avail_obj.search(cr,uid,[('default_code','=', Prod_drw_numb)])
            
            if not prod_id:
                raise osv.except_osv(_('Hi!'), _('Product is Not Available Please Wait'))
            else:
                return True
# just change the default_code to your field name and Prod_drw_numb some inside varible what is string
# or like: ('state','=', 'akarmi')
 

You can check the sourche code, and you can find a many example in sale.py for availability and check.

Bye

def button_dummy(self,cr,uid,ids,context=None)

Odoo
on 7/22/14, 8:36 AM

Thanks Klacus.......wl chk

Odoo
on 7/22/14, 9:57 AM

Your welcome!

klacus
on 7/22/14, 10:15 AM
0

Ludo - Neobis

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

Developer

Ludo - Neobis
On 7/22/14, 8:13 AM

This is some insecure programming that causes this. In your code you have:

product_avail_obj=self.pool.get('bot.info').read(cr,uid,ids,['state'],context=context)

and you automatically assume that it is going to come up with a result. In this case, you probably do not even have a single record in bot.info. So when you try the next line to access the first record, you do not even have 1 record in the list. That is what causes the error.

 

I would rewrite the expression to first check if the list contains anythiong. In this case:

if not product_avail_obj:

    raise osv.except_osv(_('Error'), _('No line for bot.info found')) 
 

About This Community

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

Question tools

1 follower(s)

Stats

Asked: 7/22/14, 6:56 AM
Seen: 420 times
Last updated: 3/16/15, 8:10 AM