Reputation: 3057
In Odoo 10-e is this possible to execute a query and then cast the resultant list or tuple into a python object. Like
self.env.cr.execute("SELECT * FROM res.users WHERE Id = 1")
In above query it would return a single record
self.env.cr.execute("SELECT * FROM res.users")
Now this query would return a list of users. Now is there any way to tell self.env.cr.fetchall()
that i want the result as a single User object or List of Users . If not then can we cast them after fetching ?
Upvotes: 3
Views: 3471
Reputation: 14776
Just use the ORM layer for those simple queries. There are some easy methods for this (e.g. for 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's domain syntax
# returns python objects if something were found
domain = [('name', '=', 'New User')]
searched_users = self.env['res.users'].search(domain)
The examples are only touching the surface. Look into Odoo's Developer Documentation for more information.
EDIT: Using the ORM layer has advantages and disadvantages. But in context of Odoo there is one really big advantage: the layer has user access control integrated. And that is just one big advantage.
Upvotes: 3
Reputation: 14746
You can get it with following way :
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 data with dictfetchall() and with using browse() method get recordset of users.
This may help you.
Upvotes: 2
Reputation: 312
There is a way to get a result in 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 user_ids
variable, you get all res.partner's ID.
Upvotes: -1