这些方法应该在表单视图上更新res.partner
名称,它会打开一个新窗口,显示合作伙伴名称和一个更新它的按钮。
最初在v8上它看起来像这样:
class WizUpdatename(osv.osv_memory):
_name = 'wiz.updatename'
_description = "Wizard that changes the partner name"
def set_name(self, cr, uid, ids, context):
""" Change value of the name field
"""
data = self.pool.get('wiz.updatename').read(cr, uid, ids)[0]
if not data['sure']:
raise osv.except_osv(
_("Error!"),
_("Please confirm that you want to do this by checking the"
" option"))
partner_obj = self.pool.get('res.partner')
name_partner = data['name']
partner_obj.write(cr, uid, context['active_id'],
{'name': name_partner}, context=context)
return {}
def _get_name(self, cr, uid, context=None):
""" Get name field value
"""
if context is None:
context = {}
partner_obj = self.pool.get('res.partner')
partner = partner_obj.search(cr, uid,
[('id', '=', context['active_id'])])
partner_o = partner_obj.browse(cr, uid, partner[0])
return partner_o and partner_o.name or False
_columns = {
'name': fields.char(
string='Name', size=256, required=True,
default=lambda s: s._get_name()),
'sure': fields.boolean('Are you sure?'),
}
WizUpdatename()
镜像后,现在看起来像这样:
class WizUpdatename(models.TransientModel):
_name = 'wiz.updatename'
_description = "Wizard that changes the partner name"
name = fields.Char(
string='Name', size=256, required=True,
default=lambda s: s._get_name())
sure =fields.Boolean(string='Are you sure?')
@api.multi
@api.depends('res.partner')
def set_name(self): #, cr, uid, ids, context
""" Change value of the name field
"""
data = self.env['wiz.updatename'].read()[0]
if not data['sure']:
raise UserError(
_("Error!"),
_("Please confirm that you want to do this by checking the"
" option"))
partner_obj = self.env['res.partner']
name_partner = data['name']
partner_obj.write(context['active_id'],
{'name': name_partner})
return {}
@api.depends('res.partner')
def _get_name(self): #, cr, uid, context=None
""" Get name field value
"""
#if context is None:
#context = {}
#ctx = self._context.get()
partner_obj = self.env['res.partner']
partner = partner_obj.search([('id', '=', 'active')])
partner_o = partner_obj.browse(partner[0])
return partner_o and partner_o.name or False
但每次我点击它,它都会抛出:
Traceback (most recent call last):
File "/home/kristian/.virtualenvs/odoov10/lib/python2.7/site-packages/odoo-10.0rc1c_20161005-py2.7.egg/odoo/http.py", line 638, in _handle_exception
return super(JsonRequest, self)._handle_exception(exception)
File "/home/kristian/.virtualenvs/odoov10/lib/python2.7/site-packages/odoo-10.0rc1c_20161005-py2.7.egg/odoo/http.py", line 675, in dispatch
result = self._call_function(**self.params)
File "/home/kristian/.virtualenvs/odoov10/lib/python2.7/site-packages/odoo-10.0rc1c_20161005-py2.7.egg/odoo/http.py", line 331, in _call_function
return checked_call(self.db, *args, **kwargs)
File "/home/kristian/.virtualenvs/odoov10/lib/python2.7/site-packages/odoo-10.0rc1c_20161005-py2.7.egg/odoo/service/model.py", line 119, in wrapper
return f(dbname, *args, **kwargs)
File "/home/kristian/.virtualenvs/odoov10/lib/python2.7/site-packages/odoo-10.0rc1c_20161005-py2.7.egg/odoo/http.py", line 324, in checked_call
result = self.endpoint(*a, **kw)
File "/home/kristian/.virtualenvs/odoov10/lib/python2.7/site-packages/odoo-10.0rc1c_20161005-py2.7.egg/odoo/http.py", line 933, in __call__
return self.method(*args, **kw)
File "/home/kristian/.virtualenvs/odoov10/lib/python2.7/site-packages/odoo-10.0rc1c_20161005-py2.7.egg/odoo/http.py", line 504, in response_wrap
response = f(*args, **kw)
File "/home/kristian/odoov10/odoo-10.0rc1c-20161005/odoo/addons/web/controllers/main.py", line 862, in call_kw
return self._call_kw(model, method, args, kwargs)
File "/home/kristian/odoov10/odoo-10.0rc1c-20161005/odoo/addons/web/controllers/main.py", line 854, in _call_kw
return call_kw(request.env[model], method, args, kwargs)
File "/home/kristian/.virtualenvs/odoov10/lib/python2.7/site-packages/odoo-10.0rc1c_20161005-py2.7.egg/odoo/api.py", line 679, in call_kw
return call_kw_model(method, model, args, kwargs)
File "/home/kristian/.virtualenvs/odoov10/lib/python2.7/site-packages/odoo-10.0rc1c_20161005-py2.7.egg/odoo/api.py", line 664, in call_kw_model
result = method(recs, *args, **kwargs)
File "/home/kristian/.virtualenvs/odoov10/lib/python2.7/site-packages/odoo-10.0rc1c_20161005-py2.7.egg/odoo/models.py", line 1101, in default_get
defaults[name] = field.default(self)
File "/home/kristian/odoov10/gilda/l10n_ve_fiscal_requirements/wizard/wizard_update_name.py", line 37, in <lambda>
default=lambda s: s._get_name())
File "/home/kristian/odoov10/gilda/l10n_ve_fiscal_requirements/wizard/wizard_update_name.py", line 67, in _get_name
partner = partner_obj.search([('id', '=', 'active')]) #cr, uid, context['active_id'] later added ctx before active_id, now, brackets removed let's see
File "/home/kristian/.virtualenvs/odoov10/lib/python2.7/site-packages/odoo-10.0rc1c_20161005-py2.7.egg/odoo/models.py", line 1497, in search
res = self._search(args, offset=offset, limit=limit, order=order, count=count)
File "/home/kristian/odoov10/odoo-10.0rc1c-20161005/odoo/addons/base/res/res_partner.py", line 621, in _search
count=count, access_rights_uid=access_rights_uid)
File "/home/kristian/.virtualenvs/odoov10/lib/python2.7/site-packages/odoo-10.0rc1c_20161005-py2.7.egg/odoo/models.py", line 4203, in _search
self._cr.execute(query_str, where_clause_params)
File "/home/kristian/.virtualenvs/odoov10/lib/python2.7/site-packages/odoo-10.0rc1c_20161005-py2.7.egg/odoo/sql_db.py", line 141, in wrapper
return f(self, *args, **kwargs)
File "/home/kristian/.virtualenvs/odoov10/lib/python2.7/site-packages/odoo-10.0rc1c_20161005-py2.7.egg/odoo/sql_db.py", line 218, in execute
res = self._obj.execute(query, params)
DataError: invalid input syntax for integer: "active"
LINE 1: ...artner"."active" = true) AND "res_partner".id = 'active') ...
错误就在此行partner = partner_obj.search([('id', '=', 'active')])
上,原来active
是active_id
,因为它应该根据该记录浏览一条记录,但在新的Odoo v10上,字段{{1 }}不再存在,有active_id
个,但我认为是以其他方式使用,明显不同于active
。
我仍然无法在v10社区找到与此字段相同的内容。
有什么想法吗?
答案 0 :(得分:1)
您无法使用字符串搜索整数字段。
您可以从上下文变量中获取活动ID以搜索所选合作伙伴。
要获取active_id ,请使用以下代码
self._context.get('active_ids')
**这是你修正的方法**
@api.depends(your_field_name)
def _get_name(self):
partner_obj = self.env['res.partner']
partner = partner_obj.search([('id', '=',self._context.get('active_ids'))],limit=1)
return partner and partner.name or False
这可能会对你有帮助。
答案 1 :(得分:1)
此处不需要search
装饰器。新API RecordSet
已经返回@api.multi
def set_name(self):
""" Change value of the name field """
self.ensure_one()
if not self.sure:
raise UserError(
_("Error!"),
_("Please confirm that you want to do this by checking the"
" option"))
partner_obj = self.env['res.partner']
partner_obj.browse(self.env.context.get('active_id', 0).write(
{'name': self.name})
return {}
@api.model # default methods don't work on records
def _get_name(self):
""" Get name field value """
partner = self.env['res.partner'].browse(
self.env.context.get('active_id', 0))
return partner.name if partner else False
,因此无需再次浏览。或者由于您已在上下文中拥有ID,因此只需使用browse。我会迁移两种方法,如:
{{1}}