콘텐츠로 건너뛰기
메뉴
커뮤니티에 참여하려면 회원 가입을 하시기 바랍니다.
신고된 질문입니다
2 답글
9046 화면

Hi,

right now I'm trying to import a class from another module and use it. The code is as following

import importlib 

cooperative_management = importlib.import_module('\odoo.addons.cooperative-management')
class ApiExample(http.Controller):    
    @http.route('/oauth-api/oauth-api/', auth='public')    
    def index(self, **kw):        
        try:            
            test = cooperative_management.controllers.quotasManagement() except Exception as e:            

body = {                 'error': {                    'code':500,                    "message":str(e),                    "type": type(e).__name__,                    "filename":__file__,                    "line": e.__traceback__.tb_lineno                }            }           

 return body


If I invoke the previous method, even with try catch, it always returns 500: internal error but doesn't return the custom error from the except.

아바타
취소
베스트 답변

Hi,

You can do it as follows:

from odoo.addons.mail.controllers.main import MailController

@http.route('/lead/case_mark_won', type='http', auth='user', methods=['GET'])
def crm_lead_case_mark_won(self, res_id, token):
comparison, record, redirect = MailController._check_token_and_record_or_redirect('crm.lead', int(res_id), token)
if comparison and record:
try:
record.action_set_won_rainbowman()
except Exception:
_logger.exception("Could not mark crm.lead as won")
return MailController._redirect_to_messaging()
return redirect

Above sample from crm module.

Thanks

아바타
취소
작성자

Hi,

Thank you so much for helping

when I try to use the from .. import class, there's an error related to the hyphen ("-") in module name. Do you know how to workaround on this? Do I have to change the module name?

you might missed to import _ ?
from odoo .import _

작성자

even importing "_" didn't solve. Well, changed the module name to use "_" instead of hyphen "-", it's now working as you suggested in your first answer.
Next time I should not use non standard characters in odoo/python modules :)

베스트 답변
You can import like this: 
from odoo.addons.custom_addon_name.controllers.your_py_file_name import class_of_function_name


아바타
취소
관련 게시물 답글 화면 활동
1
10월 20
9965
0
8월 22
3612
1
7월 25
2405
2
7월 25
7892
2
7월 25
4301