how to get access to records using Controller? Odoo 10

I'm trying to return products information via json using Controllers.

Here is what I try

class api_test(http.Controller):
    @http.route('/test', type='json', auth='public')
    def index2(self, **args):
        p = self.env['product.template'].search_read([], ['name'])
        return json.dumps(p)

But I get this error message

'api_test' object has no attribute 'env'

How can I get that Info without using json-rpc? thanks for your help

Upvotes: 1

Views: 1205

Answers (1)

I found it! I used http.request.env inside odoo module.

class api_test(http.Controller):
    @http.route('/test', type='json', auth='public')
    def index2(self, **args):
        p = http.request.env['product.template'].sudo().search_read([], ['name'])
        return json.dumps(p)

Upvotes: 2

Related Questions