Odoo Help


How to fix 'NoneType' object has no attribute '__getitem__' ?

Trai Doan
on 11/16/14, 11:24 PM 31,299 views

class vatlieu(osv.osv):
    _name = "vatlieu" 
    _columns = {
        'name': fields.char('Mã vật liệu',size=20,required=True), 
        'ten': fields.char('Tên vật liệu',size=100,required=True),


class nguyenlieu(osv.osv):
    _name = "tt.nguyenlieu" 
    _columns = {
        'name': fields.many2one('vatlieu','Nguyên liệu',required=True),
        'ten': fields.char('Tên vật liệu',size=100,required=True),
        'soluong':fields.integer('Số lượng cần',required=True),

def on_change_nguyenlieu(self, cr, uid, ids,params):
        cr.execute('select ten from vatlieu where id =%d'%int(params))
        return {'value':v}


My xml 

<record id="nguyenlieu_form" model="ir.ui.view"> 
            <field name="name">nguyenlieu_form</field> 
            <field name="model">tt.nguyenlieu</field> 
            <field name="type">form</field> 
            <field name="arch" type="xml"> 
                        <group col="2">
                            <field name="name" style="width:350px" on_change="on_change_nguyenlieu(name)"/>
                            <field name="ten" style="width:350px"/>
                            <field name="soluong" style="width:350px"/> 

When i create new nguyenlieu this error appear :

Traceback (most recent call last): File "C:\openerp-8.0rc1\openerp\http.py", line 496, in _handle_exception return super(JsonRequest, self)._handle_exception(exception) File "C:\openerp-8.0rc1\openerp\http.py", line 513, in dispatch result = self._call_function(**self.params) File "C:\openerp-8.0rc1\openerp\http.py", line 279, in _call_function return checked_call(self.db, *args, **kwargs) File "C:\openerp-8.0rc1\openerp\service\model.py", line 113, in wrapper return f(dbname, *args, **kwargs) File "C:\openerp-8.0rc1\openerp\http.py", line 276, in checked_call return self.endpoint(*a, **kw) File "C:\openerp-8.0rc1\openerp\http.py", line 729, in __call__ return self.method(*args, **kw) File "C:\openerp-8.0rc1\openerp\http.py", line 372, in response_wrap response = f(*args, **kw) File "C:\openerp-8.0rc1\openerp\addons\web\controllers\main.py", line 941, in call_kw return self._call_kw(model, method, args, kwargs) File "C:\openerp-8.0rc1\openerp\addons\web\controllers\main.py", line 933, in _call_kw return getattr(request.registry.get(model), method)(request.cr, request.uid, *args, **kwargs) File "C:\openerp-8.0rc1\openerp\api.py", line 235, in wrapper return old_api(self, *args, **kwargs) File "C:\openerp-8.0rc1\openerp\api.py", line 358, in old_api result = method(recs, *args, **kwargs) File "C:\openerp-8.0rc1\openerp\models.py", line 5686, in onchange record._onchange_eval(name, field_onchange[name], result) File "C:\openerp-8.0rc1\openerp\models.py", line 5620, in _onchange_eval method_res = getattr(self._model, method)(*args) File "C:\openerp-8.0rc1\openerp\api.py", line 235, in wrapper return old_api(self, *args, **kwargs) File "C:\openerp-8.0rc1\openerp\addons\DoanERP\core.py", line 41, in on_change_nguyenlieu ten=cr.fetchone()[0]

TypeError: 'NoneType' object has no attribute '__getitem__'

How can i fix this problem ,plz help me ,i'm new in odoo .Thanks




| 6 8 7
Bangalore, India
On 11/16/14, 11:40 PM

HI , the error is because of this line of code ten=cr.fetchone()[0]

you need to check for the value exist in the cr.fetchone() and then you can assign to a variable,


ten = ten and ten[0] or False

you are trying to convert string to integer and passing for the query

cr.execute('select ten from vatlieu where id =%d'%int(params))

or instead you can search with the name field in the query. and make sure you declare the variable v={} in the starting of your code.

thanks for your answer but error still happen .if i use ten=cr.fetchone()[0] this error always happen when i open form and after that it works fine (it returns right value). when i use your code . i can open form without error but when i select value from selection error happen and no value show .

Trai Doan
on 11/17/14, 12:56 AM

About This Community

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


Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

1 follower(s)


Asked: 11/16/14, 11:24 PM
Seen: 31299 times
Last updated: 3/16/15, 8:10 AM