Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
261 views
in Technique[技术] by (71.8m points)

python - Odoo 12 getting TypeError on while trying to access models record from record

I am trying to make a simple controller that returns a json object in Odoo 12 using Python. I have been following several tutorials, yet I keep getting the same error every time I try to use env['product.template']. Here is my controller code.

import odoo.http as http
from odoo import SUPERUSER_ID
from odoo import registry as registry_get
from odoo.api import Environment
import json


class Controller(http.Controller):

    @http.route('/test', type='http', auth='public', website=False)
    def handler(self):
        registry = registry_get('ceres')
        with registry.cursor() as cr:
            env = Environment(cr, SUPERUSER_ID, {})
            attendee = env['product.template'].sudo().search([])
        return attendee 

and here is the exception i recieve while calling the controller :

2021-01-25 08:43:08,686 5912 ERROR ceres werkzeug: Error on request:
Traceback (most recent call last):
  File "C:UsersSofianeDesktopOdoo12pythonlibsite-packageswerkzeugserving.py", line 205, in run_wsgi
    execute(self.server.app)
  File "C:UsersSofianeDesktopOdoo12pythonlibsite-packageswerkzeugserving.py", line 193, in execute
    application_iter = app(environ, start_response)
  File "C:UsersSofianeDesktopOdoo12serverodooserviceserver.py", line 342, in app
    return self.app(e, s)
  File "C:UsersSofianeDesktopOdoo12serverodooservicewsgi_server.py", line 128, in application
    return application_unproxied(environ, start_response)
  File "C:UsersSofianeDesktopOdoo12serverodooservicewsgi_server.py", line 117, in application_unproxied
    result = odoo.http.root(environ, start_response)
  File "C:UsersSofianeDesktopOdoo12serverodoohttp.py", line 1317, in __call__
    return self.dispatch(environ, start_response)
  File "C:UsersSofianeDesktopOdoo12serverodoohttp.py", line 1290, in __call__
    return self.app(environ, start_wrapped)
  File "C:UsersSofianeDesktopOdoo12pythonlibsite-packageswerkzeugwsgi.py", line 599, in __call__
    return self.app(environ, start_response)
  File "C:UsersSofianeDesktopOdoo12serverodoohttp.py", line 1490, in dispatch
    return response(environ, start_response)
TypeError: 'product.template' object is not callable - - -
question from:https://stackoverflow.com/questions/65881511/odoo-12-getting-typeerror-on-while-trying-to-access-models-record-from-record

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

you need to return html response

from odoo.http import request
import odoo.http as http
from odoo import SUPERUSER_ID
from odoo import registry as registry_get
from odoo.api import Environment
import json


class Controller(http.Controller):

    @http.route('/test', type='http', auth='public', website=False)
    def handler(self):
        registry = registry_get('ceres')
        with registry.cursor() as cr:
            env = Environment(cr, SUPERUSER_ID, {})
            attendee = env['product.template'].sudo().search([])
        response = request.make_response(attendee)
        return response 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...