Skip to Content
Menu
This question has been flagged
5 Replies
4065 Views

Hi Guys,

I have the following code to extract the details out of a record. The query worked fine for "Family", however it kept telling me that "classNumber" was not an existing field.


I spent ages trying to workout why not! However I decided to recreate the field as classnumber (all lowercase) and it works fine!! Any thoughts? Is there a way that I can leave all my fields as upper case? Otherwise I need to go through and create all lower case versions of my fields to make it work.


@api.onchange('templateProduct')

def templateProduct_onchange(self, templateProduct):

self._cr.execute("SELECT family, classNumber FROM npirequest_npirequest WHERE id="+str(templateProduct))

result = self._cr.dictfetchone()

Thanks,

Damien

Avatar
Discard

It is discouraged using SQL queries (Bypass the ORM).

> You should never use the database cursor directly when the ORM can do the same thing! By doing so you are bypassing all the ORM features, possibly the transactions, access rights and so on.

Source: https://www.odoo.com/documentation/10.0/reference/guidelines.html#programming-in-odoo

Thanks komit for giving this link www.odoo.com/documentation/10.0/reference/guidelines.html#programming-in-odoo .


Best Answer

The answer that was given from 

Bharat Parmar (bhp) 

actually explains how its done from an SQL query. Although odoo is allowing us to use the ORM (Object Relational Mapping) without the need of query. The query is prepared by the ORM and what is most important is that it is NOT BYpassing the ACL's (access control lists) and constraints. This means that if an unauthorized user tries to execute a command, he will get the warning message that he doesnt have the proper access rights. But if you give him the raw query, then everyone can touch the info. 

So to translate this:

self._cr.execute("SELECT family, classNumber FROM npirequest_npirequest WHERE id="+str(templateProduct))

into a ORM would be:

recs = self.env['npirequest.npirequest'].search([('id','=','templateProduct')])

for rec in recs:

     print family, classNumber

Would do the trick, or change in the search the criteria that you need.

Hope this is helpful.

Riste Kabranov

Python/Odoo Developer @ NEBIZ IT DOOEL

Avatar
Discard
Best Answer

I think SQL query is case-insensitive. so if you write column name like classNumber and classnumber in query there is no difference between them.

So in python module you must declare all fields in small letters only.

Ex.

if you declare field like this,

name = fields.Char()

and in SQL query you can use that name field in any case like "name","NAME","NamE". This all works in SQL query.

But, if you declare field with any capital letter like this,

Name = fields.Char()

then in SQL query you can't access this field as a column in any way. If you try to declare SQL query with "Name" then it finds "name" in database.

I hope this will help you.

Avatar
Discard
Author Best Answer

Thanks for the advice.  I have changed my variables to all lower case it now works perfectly fine.  I have also noted the point about using the ORM instead, however at the time, using directly SQL was only way that I could access this table.  Thanks for the advise!

Avatar
Discard