Skip to Content
Menu
This question has been flagged
1 Reply
68707 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

Avatar
Discard
Best Answer

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.

Avatar
Discard
Author

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 .