openerp Message_post error..NameError: global name '_' is not defined

主宰稳场 提交于 2019-12-24 02:17:28

问题


I have created a custom module with chatter in the form.

I want to post a custom message. but I am getting following error.

File "/opt/openerp/my_modules/forum/forum.py", line 22, in function_which_post_msg
    self.message_post(cr, uid, ids, body=_("New Question has been <b>created</b>"), context=context)
NameError: global name '_' is not defined

my .py file is

import datetime
import time
import openerp
from openerp.osv import osv, fields


class Course(osv.osv):
    _name = "forum.course"
    _inherit = ['mail.thread', 'ir.needaction_mixin']

    _columns = {
              'name' : fields.char(string="Question Title", size=256, required=True),
              'description' : fields.text(string="Question Description", required=True),

               }


    def function_which_post_msg(self, cr, uid, ids, context=None):
        self.message_post(cr, uid, ids, body=_("New Question has been <b>created</b>"), context=context)

    def create(self, cr, uid, ids, context=None):
        self.function_which_post_msg(cr, uid, ids, context=context)

回答1:


try this to import in your .py file

from openerp import tools

from openerp.tools.translate import _

_ is used for a translation the message when active user's language is changed according to. It's translate the message as in your .po file.



来源:https://stackoverflow.com/questions/21671417/openerp-message-post-error-nameerror-global-name-is-not-defined

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!