Odoo - Write an entry and pass it to a python object
In Odoo 10th it is possible to execute a query and then pass the resulting list or tuple into a python object. how
self.env.cr.execute("SELECT * FROM res.users WHERE Id = 1")
The above query will return one record
self.env.cr.execute("SELECT * FROM res.users")
Now this query will return a list of users. Now there is some way to say self.env.cr.fetchall()
that I want the result to be a single user object or list of users. If not, then can we discard them after retrieving?
source to share
Just use ORM layer for these simple queries. There are several easy ways to do this (for example, for a typical CRUD-> create, read, update, delete):
Create -> create (vals)
# creates a user in db and
# returns this user as python object (RecordSet)
created_user = self.env['res.users'].create({'name': 'New User'})
Browse -> browse (list_of_ids)
# reads the whole database rows and
# returns python objects (RecordSet)
browsed_users = self.env['res.users'].browse([1,2])
Search -> search (domain)
# search database with Odoo domain syntax
# returns python objects if something were found
domain = [('name', '=', 'New User')]
searched_users = self.env['res.users'].search(domain)
The examples only concern the surface. See the Odoo developer documentation for details .
EDIT: Using an ORM layer has its advantages and disadvantages. But in the Odoo context, there is one really big advantage: the user access control layer is integrated. And that's just one big advantage.
source to share
You can get it like this:
q = "select * from res_users where id = 1"
#q = "select * from res_users"
self.env.cr.execute(q)
res = self.env.cr.dictfetchall()
users = self.env['res.users'].browse([row['id'] for row in res])
Get the data using dictfetchall () and use the browse () method to get the user recordset.
This might help you.
source to share
There is a way to get the result in a python list object.
qry = """select id from res_partner where parent_id is Null;"""
self._cr.execute(qry)
result = self._cr.dictfetchall()
user_ids=[]
for ids in result:
user_ids.append(ids.get('id'))
In the variable user_ids
you get the id res.partner.
source to share