3

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 ?

2
  • Why not using the ORM layer instead? Sorry, but i don't see the need of bypassing the ORM layer with such an example. Commented Jul 20, 2017 at 7:22
  • @CZoellner Actually i am new to Odoo and i am unaware how can you fetch those with ORM Commented Jul 20, 2017 at 12:02

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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.

1 Comment

That looks good, can i also do something more with this browse ? How can i only those users which are in Group 1. Assuming that Group 1 is a valid user and there are some users in that group
-1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.