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 'NoneType' object has no attribute '__getitem__' ?

By
Trai Doan
on 11/16/14, 11:24 PM 13,064 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),

vatlieu()

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))
        ten=cr.fetchone()[0]
        v={'ten':ten}
        return {'value':v}

nguyenlieu()

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"> 
                <form> 
                        <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"/> 
                        </group>
                </form> 
            </field> 
        </record>

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

1

Maniganda

--Maniganda--
1038
| 5 7 7
Bangalore, India
--Maniganda--
Maniganda
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=cr.fetchone()

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

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/16/14, 11:24 PM
Seen: 13064 times
Last updated: 3/16/15, 8:10 AM