Skip to Content
Odoo Menu
  • Prihlásiť sa
  • Vyskúšajte zadarmo
  • Aplikácie
    Financie
    • Účtovníctvo
    • Fakturácia
    • Výdavky
    • Tabuľka (BI)
    • Dokumenty
    • Podpis
    Predaj
    • CRM
    • Predaj
    • POS Shop
    • POS Restaurant
    • Manažment odberu
    • Požičovňa
    Webstránky
    • Tvorca webstránok
    • eShop
    • Blog
    • Fórum
    • Živý chat
    • eLearning
    Supply Chain
    • Sklad
    • Výroba
    • Správa životného cyklu produktu
    • Nákup
    • Údržba
    • Manažment kvality
    Ľudské zdroje
    • Zamestnanci
    • Nábor zamestnancov
    • Voľné dni
    • Hodnotenia
    • Odporúčania
    • Vozový park
    Marketing
    • Marketing sociálnych sietí
    • Email marketing
    • SMS marketing
    • Eventy
    • Marketingová automatizácia
    • Prieskumy
    Služby
    • Projektové riadenie
    • Pracovné výkazy
    • Práca v teréne
    • Helpdesk
    • Plánovanie
    • Schôdzky
    Produktivita
    • Tímová komunikácia
    • Schvalovania
    • IoT
    • VoIP
    • Znalosti
    • WhatsApp
    Third party apps Odoo Studio Odoo Cloud Platform
  • Priemyselné odvetvia
    Retail
    • Book Store
    • Clothing Store
    • Furniture Store
    • Grocery Store
    • Hardware Store
    • Toy Store
    Food & Hospitality
    • Bar and Pub
    • Reštaurácia
    • Fast Food
    • Guest House
    • Beverage distributor
    • Hotel
    Reality
    • Real Estate Agency
    • Architecture Firm
    • Konštrukcia
    • Estate Managament
    • Gardening
    • Property Owner Association
    Poradenstvo
    • Accounting Firm
    • Odoo Partner
    • Marketing Agency
    • Law firm
    • Talent Acquisition
    • Audit & Certification
    Výroba
    • Textile
    • Metal
    • Furnitures
    • Jedlo
    • Brewery
    • Corporate Gifts
    Health & Fitness
    • Sports Club
    • Eyewear Store
    • Fitness Center
    • Wellness Practitioners
    • Pharmacy
    • Hair Salon
    Trades
    • Handyman
    • IT Hardware and Support
    • Solar Energy Systems
    • Shoe Maker
    • Cleaning Services
    • HVAC Services
    Iní
    • Nonprofit Organization
    • Environmental Agency
    • Billboard Rental
    • Photography
    • Bike Leasing
    • Software Reseller
    Browse all Industries
  • Komunita
    Vzdelávanie
    • Tutoriály
    • Dokumentácia
    • Certifikácie
    • Školenie
    • Blog
    • Podcast
    Empower Education
    • Vzdelávací program
    • Scale Up! Business Game
    • Visit Odoo
    Softvér
    • Stiahnuť
    • Porovnanie Community a Enterprise vierzie
    • Releases
    Spolupráca
    • Github
    • Fórum
    • Eventy
    • Preklady
    • Staň sa partnerom
    • Services for Partners
    • Register your Accounting Firm
    Služby
    • Nájdite partnera
    • Nájdite účtovníka
    • Meet an advisor
    • Implementation Services
    • Zákaznícke referencie
    • Podpora
    • Upgrades
    ​Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Získajte demo
  • Cenník
  • Pomoc

Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:

  • CRM
  • e-Commerce
  • Účtovníctvo
  • Sklady
  • PoS
  • Projektové riadenie
  • MRP
All apps
You need to be registered to interact with the community.
All Posts People Badges
Tagy (View all)
odoo accounting v14 pos v15
About this forum
You need to be registered to interact with the community.
All Posts People Badges
Tagy (View all)
odoo accounting v14 pos v15
About this forum
Pomoc

How to invoke a Controller function from inside a Model function ?

Odoberať

Get notified when there's activity on this post

This question has been flagged
modelsession_idcontrollerroute
18 Replies
23596 Zobrazenia
Avatar
PY

Hi !

I basically try to invoke a function from within a Controller that does exactly what I want. But, I can't reach it, neither by importing this controller, nor by using it's route.

To use the function directly, I need an instance of this controller.

To use the route method, I tried using Requests (Python) and a simple get on the route. However, I need the session_id in the headers. And I can't retrieve that info within the Model function, where I am.

I tried with request.session_id, but I got this error:

Traceback (most recent call last):
File "/home/odoo/server/openerp/service/server.py", line 929, in preload_registries
registry = RegistryManager.new(dbname, update_module=update_module)
File "/home/odoo/server/openerp/modules/registry.py", line 370, in new
openerp.modules.load_modules(registry._db, force_demo, status, update_module)
File "/home/odoo/server/openerp/modules/loading.py", line 351, in load_modules
force, status, report, loaded_modules, update_module)
File "/home/odoo/server/openerp/modules/loading.py", line 255, in load_marked_modules
loaded, processed = load_module_graph(cr, graph, progressdict, report=report, skip_modules=loaded_modules, perform_checks=perform_checks)
File "/home/odoo/server/openerp/modules/loading.py", line 176, in load_module_graph
_load_data(cr, module_name, idref, mode, kind='data')
File "/home/odoo/server/openerp/modules/loading.py", line 118, in _load_data
tools.convert_file(cr, module_name, filename, idref, mode, noupdate, kind, report)
File "/home/odoo/server/openerp/tools/convert.py", line 901, in convert_file
convert_xml_import(cr, module, fp, idref, mode, noupdate, report)
File "/home/odoo/server/openerp/tools/convert.py", line 987, in convert_xml_import
obj.parse(doc.getroot(), mode=mode)
File "/home/odoo/server/openerp/tools/convert.py", line 853, in parse
self._tags[rec.tag](self.cr, rec, n, mode=mode)
File "/home/odoo/server/openerp/tools/convert.py", line 379, in _tag_function
_eval_xml(self,rec, self.pool, cr, uid, self.idref, context=context)
File "/home/odoo/server/openerp/tools/convert.py", line 230, in _eval_xml
res = getattr(model, method)(cr, uid, *args)
File "/home/odoo/server/openerp/api.py", line 241, in wrapper
return old_api(self, *args, **kwargs)
File "/home/odoo/server/openerp/api.py", line 336, in old_api
result = method(recs, *args, **kwargs)
File "/home/odoo/my_addons/vimexcom/models/company.py", line 62, in set_params
headers = {'cookie': 'session_id=%s' % request.session_id}
File "/usr/lib/python2.7/dist-packages/werkzeug/local.py", line 336, in __getattr__
return getattr(self._get_current_object(), name)
File "/usr/lib/python2.7/dist-packages/werkzeug/local.py", line 295, in _get_current_object
return self.__local()
File "/usr/lib/python2.7/dist-packages/werkzeug/local.py", line 131, in _lookup
raise RuntimeError('object unbound')
ParseError: "object unbound" while parsing /home/odoo/my_addons/vimexcom/data/data.xml:10, near
<function model="res.company" name="set_params"/>

Any tips would be appreciated.

Thanks !

0
Avatar
Zrušiť
Sehrish

hope this will helps: https://learnopenerp.blogspot.com/2020/08/unbound-method-call-controller-function-from-another-controller-odoo.html

Avatar
Temur
Best Answer

if it may help you, then you can get session id as follows:

from openerp import http


session_id = http.request.session.sid


UPDATE:

Controllers are registering itself in the openerp.http.controllers_per_module dict (see code)... so you can get controller instance using module name and controller name under it:

from openerp.http import controllers_per_module

controller_instance = None
for name,instance in controllers_per_module.get('my_module'): if name.endswith('my_module.controller.name'): controller_instance = instance break if controller_instance != None: controller_instance.a_function(*args)




1
Avatar
Zrušiť
PY
Autor

I tried that, but I got an error (can't remember the error).

PY
Autor

Updated my question.

Temur

http.request.session.sid gives you a session id string.

PY
Autor

Same error with request.session.sid

Temur

ok. it seems session id does not helps

PY
Autor

thanks anyway, I'll find a workaround one day, I'm sure ^^

Temur

you mention direct call, what is problem with it?

PY
Autor

Oh, yeah, if I import the controller (from addons/website/controllers/main import Website) and use it (let's say ... Website.some_function(*args)), I get a "unbound method blablabla must be called with Website instance as first argument ..." But, hey, where do I find that instance ? Can I create one ?

Temur

actually you can create, if you'll have all arguments to "constructor" if required, if no such requirement then it may be as simple as:

Website().some_function(*args)), 
PY
Autor

Oh yeah, I'm so stupid. And I pretend to be a developper ? Thanks, I'll try that on Monday ! Good we !

Temur

for ordinary python object it works, I don't know if it'll work for a controller without side effects :) Good we!

Temur

As I doubted it has side effect to create controller instance, it registering itself in a global dict.. and you can get existing instance from there... see updated answer.

PY
Autor

Ok, so far, that's an amazing answer ! But, what if the controller is not registered ? Can I safely instanciate one ?

Temur

Normally it should be registered. as we can see in the code, all direct children of a "Controller" (i.e. "http.Controller") class are registered. So, if a controller class you're interested in is a child of the Controller class, then it's surely registered there. you'll need just figure out it's full name under it's own module in order to find it in the "controllers_per_module". Try to print or log the "controllers_per_module" variable and check if it's a case. Most probably it'll be registered. if not, then you can try to instantiate one and check if it's a safe operation...

PY
Autor

Ok, no, nothing works, and I think it's because my method is called on module update, so, maybe not all the controllers are instanciated at this moment (the Website controller specifically). But, hey, your answer is the right one I think, and I'll accept that. And, for the record, I just copy/pasted/shortened the content of the method I wanted to call so... problem solved.

Avatar
Axel Mendoza
Best Answer

Maybe you need to call your function in another moment, like after the models registry(pool) is loaded. You could do this by implementing the method _register_hook in your model. Odoo will always call that method in your model to allow you to initialize whatever you want, but you just have the self and cr arguments.

Maybe helps

1
Avatar
Zrušiť
Enjoying the discussion? Don't just read, join in!

Create an account today to enjoy exclusive features and engage with our awesome community!

Registrácia
Related Posts Replies Zobrazenia Aktivita
session_id as GET parameter in URL returns 404
session_id controller route
Avatar
Avatar
Avatar
3
okt 15
12849
Get current model informations in the controller Solved
model controller
Avatar
Avatar
2
júl 24
3443
http.route(... auth="none") not working in Odoo11 Solved
controller route
Avatar
4
okt 20
6917
Problem with the controller routes
controller route
Avatar
Avatar
2
mar 16
5181
Get model data in a controller Solved
orm model controller
Avatar
Avatar
Avatar
Avatar
4
okt 21
38536
Komunita
  • Tutoriály
  • Dokumentácia
  • Fórum
Open Source
  • Stiahnuť
  • Github
  • Runbot
  • Preklady
Služby
  • Odoo.sh hosting
  • Podpora
  • Vyššia verzia
  • Custom Developments
  • Vzdelávanie
  • Nájdite účtovníka
  • Nájdite partnera
  • Staň sa partnerom
O nás
  • Naša spoločnosť
  • Majetok značky
  • Kontaktujte nás
  • Pracovné ponuky
  • Eventy
  • Podcast
  • Blog
  • Zákazníci
  • Právne dokumenty • Súkromie
  • Bezpečnosť
الْعَرَبيّة Català 简体中文 繁體中文 (台灣) Čeština Dansk Nederlands English Suomi Français Deutsch हिंदी Bahasa Indonesia Italiano 日本語 한국어 (KR) Lietuvių kalba Język polski Português (BR) română русский язык Slovenský jazyk slovenščina Español (América Latina) Español ภาษาไทย Türkçe українська Tiếng Việt

Odoo je sada podnikových aplikácií s otvoreným zdrojovým kódom, ktoré pokrývajú všetky potreby vašej spoločnosti: CRM, e-shop, účtovníctvo, skladové hospodárstvo, miesto predaja, projektový manažment atď.

Odoo prináša vysokú pridanú hodnotu v jednoduchom použití a súčasne plne integrovanými biznis aplikáciami.

Website made with

Odoo Experience on YouTube

1. Use the live chat to ask your questions.
2. The operator answers within a few minutes.

Live support on Youtube
Watch now