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不可序列化

因为from
product.template
如果传递对象
,那么如果print
self.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