Object 如何从Odoo 10中的对象获取Id?
这是我的结构:Object 如何从Odoo 10中的对象获取Id?,object,openerp,self,odoo-10,Object,Openerp,Self,Odoo 10,这是我的结构: class Imprint_Location(models.Model): _name = 'imprint.location' name = fields.Char() product_id = fields.Many2one('product.template') class Imprint_Charges(models.Model): _name = 'imprint.charge' _rec_name = 'location_i
class Imprint_Location(models.Model):
_name = 'imprint.location'
name = fields.Char()
product_id = fields.Many2one('product.template')
class Imprint_Charges(models.Model):
_name = 'imprint.charge'
_rec_name = 'location_id'
product_id_c = fields.Many2one('product.template', required=True)
location_id = fields.Many2one('imprint.location', required=True)
@api.multi
@api.onchange('product_id_c', 'location_id')
def product_filter(self):
res = {}
print '\n\n-------\n\n', self, self.product_id_c, '\n\n-------\n\n'
if self.product_id_c:
res['domain'] = {'location_id': [('product_id', '=', self.product_id_c.id)]}
print res
return res
class Product_Template(models.Model):
_inherit = 'product.template'
imprint_location_ids = fields.One2many('imprint.location', 'product_id')
sale_imprint_charge_ids = fields.One2many('imprint.charge', 'product_id_c')
现在,我已经在product.template
中定义了一个页面,页面内部是sale\u print\u charge\u ids
,它位于
中,我没有选择product\u id\u c
字段[而且该字段也不会显示在定义的树中]
现在我的问题是,当我从我为imprint.charge
定义的表单视图中选择此选项时,方法product\u filter
工作正常,但当我从product.template
输入时,我会得到一个错误提示
TypeError:JSON不可序列化
因为fromproduct.template
如果传递对象
,那么如果printself.product\u id\u c
则它会打印product.template()
,因此这是不可序列化的。我尝试过使用self.product\u id\u c.id
,它会给输出一个空列表[]
那么,如何从对象中获取
product.template
id,或者将id
本身传递给某个方法。当创建一个全新的记录时,Odoo会创建这个wierd
对象。写入记录后,此id将转换为您使用的普通id(整数)。在这种情况下,您有一个函数(并非不合理地)在实际不存在真实id值的情况下期望实际id值。您需要提供回退,例如评估id是否为整数,并在这种情况下提供备用值。虽然您的函数似乎返回了一个对象,但我不太知道您希望发生什么。如果您希望修改其中一个字段的值,我会修改self对象的值,而不是返回一个对象 您应该改进以下几点
- res['domain']={'location\u id':[('product\u id','=',self.product\u id\u c.id)]]
- 返回res
- ORM的几种搜索方法研究
@api.multi
@api.onchange('product_id_c', 'location_id')
def product_filter(self):
res = {}
if self.product_id_c:
self.location_id = False
#search product_template in imprint.locationwith table and limit we will get only record if related record found
location_id = self.env['imprint.location'].search([('product_id', '=', self.product_id_c.id)], limit=1)
if location_id:
#location_id.ids will give you something like [2] so we need to set value as 2
self.location_id = location_id.ids[0]
编辑:
根据您的第一个评论,您需要一个相关位置的列表,然后我们应该遵循以下技巧
- 删除产品过滤器()方法
- 在imprint.charge对象视图文件中添加域
<field name="location_id" domain="[('product_id', '=', product_id_c)]"/>
然后,重新启动Odoo服务器并升级自定义模块。我需要筛选字段
位置\u id
。。。我只想显示该产品的字段…结果可能是一个列表,我希望用户选择位置\u id
从产品模板中选择Still。我得到一个空记录集,它传递对象
而不是id
。